【问题标题】:cannot call NSMetadataQueryDidUpdateNotification无法调用 NSMetadataQueryDidUpdateNotification
【发布时间】:2014-02-22 04:50:43
【问题描述】:

我想使用NSMetadataQuery 跟踪我上传到 icloud 的文件的百分比,但它不起作用。

这是我的代码:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
    NSFileCoordinator* fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
    [fileCoordinator coordinateReadingItemAtURL:backupUrl options:NSFileCoordinatorReadingWithoutChanges error:nil byAccessor:^(NSURL *newURL) {
        NSFileManager*  fm = [NSFileManager defaultManager];
        NSError *theError = nil;

        BOOL success =[fm setUbiquitous:YES itemAtURL:backupUrl destinationURL:[[ubiq URLByAppendingPathComponent:@"Documents" isDirectory:true] URLByAppendingPathComponent:bName] error:&theError];

        if (!(success)) {
            [progView dismiss];
            UIAlertView* alertFail=[[UIAlertView alloc]initWithTitle:@"Backup Error" message:@"Could not backup to iCloud." delegate:Nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alertFail show];
            NSLog(@"iCloud error: %@", [theError localizedDescription]);
        }
        else{
            NSURL* destURL=[[ubiq URLByAppendingPathComponent:@"Documents" isDirectory:true] URLByAppendingPathComponent:bName];
            NSMetadataQuery* query=[[NSMetadataQuery alloc]init];
            [query setPredicate:[NSPredicate predicateWithFormat:@"%K > 0",NSMetadataUbiquitousItemPercentUploadedKey]];
            [query setSearchScopes:@[destURL]];
            [query setValueListAttributes:@[NSMetadataUbiquitousItemPercentUploadedKey,NSMetadataUbiquitousItemIsUploadedKey]];

            _alertQuery=query;
            [query enableUpdates];
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(liveupdate:) name:NSMetadataQueryDidUpdateNotification object:query];
`//                [progView dismiss];
            NSLog(@"desturl %@",query);
            [query  startQuery];
        }
    }];

-(void)liveupdate:(NSNotification *)note{
NSMetadataQuery* query=[note object];
if ([query resultCount]==0)
    return;

NSMetadataItem* item=[query resultAtIndex:0];
float progress=[[item valueForAttribute:NSMetadataUbiquitousItemPercentUploadedKey]floatValue];

[progView.progBar setProgress:progress animated:NO];

if ([[item valueForAttribute:NSMetadataUbiquitousItemIsUploadedKey] boolValue]){
    [query stopQuery];
    [query disableUpdates];
    _alertQuery=nil;
    [progView dismiss];
}

}

liveUpdate:note 方法没有被调用。有人可以帮助我如何修复此代码。谢谢你

我编辑了我的代码...

这是我的新代码

- (void)loadNotes:(NSString *)bname {
self.alertQuery = [[NSMetadataQuery alloc] init];
[self.alertQuery setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE %@", NSMetadataItemFSNameKey, bname]];
[self.alertQuery setSearchScopes:@[NSMetadataQueryUbiquitousDataScope]];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(liveupdate:) name:NSMetadataQueryDidUpdateNotification object:self.alertQuery];
[self.alertQuery startQuery];
}
-(void)liveupdate:(NSNotification *)note {
    NSMetadataQuery* query=[note object];
    if ([query resultCount]==0){
        return;
    }
    NSMetadataItem* item=[query resultAtIndex:0];
    float progress=[[item valueForAttribute:NSMetadataUbiquitousItemPercentUploadedKey]floatValue];


[progView.progBar setProgress:progress animated:NO];

if ([[item valueForAttribute:NSMetadataUbiquitousItemIsUploadedKey] boolValue]){
    [query stopQuery];
    [query disableUpdates];
    _alertQuery=nil;
    [progView dismiss];
}
}

它仍然无法调用 liveupdate 方法。 我的代码有什么问题?

【问题讨论】:

    标签: icloud nsmetadataquery


    【解决方案1】:

    您的元数据查询似乎存在一些问题。第一:

    [query setPredicate:[NSPredicate predicateWithFormat:@"%K > 0",NSMetadataUbiquitousItemPercentUploadedKey]];
    

    这可能是应该的,但我对此表示怀疑。您真正需要的是使用文件名的谓词。如果文件名保存在名为filenameNSString 中,则如下所示:

    [query setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE %@", NSMetadataItemFSNameKey, filename]];
    

    这是最大的问题。修复它可能就是您所需要的。但还有一些其他的事情我也会改变。下一个:

    [query setSearchScopes:@[destURL]];
    

    同样,这可能是应该起作用的东西,但我只看到了更一般的设置的好结果:

    [query setSearchScopes:@[NSMetadataQueryUbiquitousDataScope]];
    

    最后:

    [query setValueListAttributes:@[NSMetadataUbiquitousItemPercentUploadedKey,NSMetadataUbiquitousItemIsUploadedKey]];
    

    如果您通过valueListAttributes 直接在查询对象上查找值,这可能会起作用。但我建议完全放弃这条线。没有它,您仍然可以从NSMetadataItem 查找进度和状态。

    【讨论】:

    • 我更新了我的代码,但仍然无法调用 liveupdate 方法。
    猜你喜欢
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    相关资源
    最近更新 更多