nsmetadataquery没有完成聚会(无通知)
-
25-10-2019 - |
题
我正在为我的应用程序(通过iCloud)制作备份管理器。我做了一些测试,基础知识奏效了。但是几天后它停止了。我在用着 NSMetadataQuery
搜索是否存在备份文件。我的备份文件被命名 Backup29112011154133.xml
数字表示备份日期(格式为 ddMMyyyyHHmmss
)。我检查一下 -viewDidAppear
:
- (void)viewDidAppear:(BOOL)animated {
[self checkForRemoteFile];
}
- (void)checkForRemoteFile {
NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if (ubiq) {
NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
[query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"%K like 'Backup*'",NSMetadataItemFSNameKey];
[query setPredicate:pred];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:query];
[query startQuery];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iCloud is unavailable at the moment" message:nil delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
[alert setTag:TAG_ALERT_NOICLOUD];
[alert show];
}
}
- (void)queryDidFinishGathering:(NSNotification *)notif {
NSMetadataQuery *query = [notif object];
[query disableUpdates];
[query stopQuery];
[self loadRemoteFile:query];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:query];
}
- (void)loadRemoteFile:(NSMetadataQuery *)query {
if ([query resultCount] == 1) {
canRestore = YES;
NSMetadataItem *item = [query resultAtIndex:0];
// parse the backup file
[self.tableView reloadData];
} else {
canRestore = NO;
modifDate = @"never";
backupInfoLoaded = YES;
[self.tableView reloadData];
}
}
问题是 - (void)queryDidFinishGathering:(NSNotification *)notif
永远不会执行。我在那里放了brexpints和nslogs ion,但没有任何事情发生。
我还试图检查其他通知,例如“查询确实开始收集”和“查询过程”。仅发布“查询确实启动”通知。
我还拥有附有iCloud注册和权利文件的appid。
你能帮我做什么吗?也许我错过了什么?
解决方案 2
通过创建修复 ivar
为了 NSMetadataQuery
.
我不知道为什么如果没有nsmetadataquery Ivar,该应用程序将无法读取数据。
其他提示
首先,如果不从Mainthread调用Startquery,则NSMetadataQuery无效。每条路径也有可能失败。以下代码对我有用。
NSURL *mobileDocumentsDirectoryURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
...
query.predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%%K like \"%@*\"", [mobileDocumentsDirectoryURL path]], NSMetadataItemPathKey];
[query startQuery];
不幸的是,iCloud有很多问题 NSMetaDataQuery
. 。老实说,到目前为止,您所有与iCloud相关的问题的最佳来源是Apple开发人员论坛。今天,苹果发布了iOS 5.1 beta,发行笔记 仍然 假设Nsmetadataquery无法正常运行。 iCloud仍然无法正常工作,这是非常令人沮丧的,但是可悲的是我们无能为力。
这个问题仍然存在。我已经能够将其追踪到以下分歧:
- 如果将查询上的搜索谓词限制为名称键,
例如
[NSPredicate predicateWithFormat:@"%K like[cd] %@", NSMetadataItemFSNameKey, @"*"]
然后它将按预期工作(发布所有四个查询生命周期通知)。
- 但是,如果您尝试使用复合谓词或尝试与路径一起工作,
如
[NSPredicate predicateWithFormat:@"%K BEGINSWITH %@", NSMetadataItemPathKey, [self cloudDocumentsURL].path]
或者
[NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:namePred, pathPred, nil]];
然后,只有初始通知将发布。
在过去的一年中,我尝试在多个测试和预期生产应用程序中尝试了数百种可配置变量的组合,但尚未找到对此假设的反例。
不幸的是,nsmetadataquery只是无处不在的商店不起作用(截至10.8)。
我的解决方法是从查询中获得原始结果,并主要在绑定的NsarrayController上工作,该controller可以过滤其结果。这将意味着将大多数现有代码的重新分解。归因于(大概是),但这是我发现的唯一方法。我会喜欢另一种选择。