我正在为我的应用程序制作一个备份管理器(通过 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
永远不会被执行。我在那里放置了断点和 NSLogs ion,但什么也没发生。
我还尝试检查其他通知,例如“查询确实开始收集”和“查询过程”。仅发布“查询已开始”通知。
我还注册了 iCloud 的 AppID 并附加了权利文件。
你能帮我看看发生了什么事吗?也许我错过了什么?
首先,如果 startQuery 不是从 MaintThread 调用的,则 NSMetadataQuery 不起作用。
每个路径的谓词也有可能失败。
以下代码对我有用。
NSURL *mobileDocumentsDirectoryURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
...
query.predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%%K like \"%@*\"", [mobileDocumentsDirectoryURL path]], NSMetadataItemPathKey];
[query startQuery];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)