无法调用NSMetadataQueryDidUpdateNotification [英] cannot call NSMetadataQueryDidUpdateNotification

查看:104
本文介绍了无法调用NSMetadataQueryDidUpdateNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用NSMetadataQuery跟踪上传到icloud的文件的百分比,但是没有用.

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

这是我的代码:

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方法未调用.有人可以帮助我如何解决此代码.谢谢

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

我编辑了我的代码...

i edited my 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];
}
}

它仍然不能调用liveupdate方法. 我的代码有什么问题?

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

推荐答案

元数据查询似乎有些问题.首先:

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

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

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

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

最后:

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

如果您通过valueListAttributes直接在查询对象上查找值,这可能会有效.但我建议您完全删除这行.您仍然可以从NSMetadataItem查找进度和状态,而无需它.

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.

这篇关于无法调用NSMetadataQueryDidUpdateNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆