cannot call NSMetadataQueryDidUpdateNotification

喜你入骨 提交于 2019-12-13 02:27:21

问题


I want to track percentage of my uploaded file to icloud using NSMetadataQuery, but It didn't work.

This is my code :

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];
}

}

the liveUpdate:note method didn't called. can someone help me how to fix this code. thank you

i edited my code...

this is my new code

- (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];
}
}

it still can't call the liveupdate method. what is the problem with my code?


回答1:


Looks like you have some problems with your metadata query. First:

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

It might be that this is supposed to work, but I'm pretty skeptical of that. What you really need here is a predicate that uses the file name. If the filename is saved in an NSString named filename, something like this:

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

That's the biggest problem. Fixing it might be all you need. But there are some other things I'd change as well. Next:

[query setSearchScopes:@[destURL]];

Again, that might be something that's supposed to work, but I've only seen good results with a much more general setting:

[query setSearchScopes:@[NSMetadataQueryUbiquitousDataScope]];

Finally:

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

This would probably work if you looked up the values directly on the query object via valueListAttributes. But I'd recommend just dropping this line completely. You can still look up the progress and status from NSMetadataItem without it.



来源:https://stackoverflow.com/questions/21447105/cannot-call-nsmetadataquerydidupdatenotification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!