当我禁用 iCloud 时,我似乎无法让 NSMetadataQuery 工作。我输入了一个有效的搜索 URL,但它从未注册为已完成:
//Check for iCloud
NSURL *ubiq = [[NSFileManager defaultManager]
URLForUbiquityContainerIdentifier:nil];
if (ubiq) {
NSLog(@"iCloud access at %@", ubiq);
self.query = [[[NSMetadataQuery alloc] init] autorelease];
[self.query setSearchScopes:[NSArray arrayWithObject:
NSMetadataQueryUbiquitousDataScope]];
_isiCloudEnabled = YES;
} else {
NSLog(@"No iCloud access");
//Get the doc directory
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
self.query = [[[NSMetadataQuery alloc] init] autorelease];
[self.query setSearchScopes:[NSArray arrayWithObjects:
[NSURL fileURLWithPath:path],nil]];
_isiCloudEnabled = NO;
}
NSPredicate *pred = [NSPredicate predicateWithFormat:
@"%K like %@", NSMetadataItemFSNameKey, @"*.adoc"];
[self.query setPredicate:pred];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(queryDidFinishGathering:)
name:NSMetadataQueryDidFinishGatheringNotification
object:self.query];
[self.query startQuery];
queryDidFinishGathering:永远不会被调用。启用 iCloud 后,它总是会被调用。知道为什么吗?