こんにちは友人私はNSMetaDataQueryの結果が空白であり、私はこのリンクを分析しました。私は混乱しています。助けてください、ここにメタクエリのソースコードを追加します
- (NSMetadataQuery *)documentQuery {
NSMetadataQuery * query = [[NSMetadataQuery alloc] init];
if (query) {
// Search documents subdir only
[query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDataScope]];
// Add a predicate for finding the documents
NSString * filePattern = [NSString stringWithFormat:@"*.%@", PTK_EXTENSION];
[query setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE %@",
NSMetadataItemFSNameKey, filePattern]];
}
return query;
}
- (void)stopQuery {
if (_query) {
NSLog(@"No longer watching iCloud dir...");
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidUpdateNotification object:nil];
[_query stopQuery];
_query = nil;
}
}
- (void)startQuery {
[self stopQuery];
NSLog(@"Starting to watch iCloud dir...");
_query = [self documentQuery];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(processiCloudFiles:)
name:NSMetadataQueryDidFinishGatheringNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(processiCloudFiles:)
name:NSMetadataQueryDidUpdateNotification
object:nil];
[_query startQuery];
}
- (void)processiCloudFiles:(NSNotification *)notification {
// Always disable updates while processing results
[_query disableUpdates];
[_iCloudURLs removeAllObjects];
// The query reports all files found, every time.
for (NSMetadataItem *result in _query.results) {
NSURL * fileURL = [result valueForAttribute:NSMetadataItemURLKey];
NSNumber * aBool = nil;
// Don't include hidden files
[fileURL getResourceValue:&aBool forKey:NSURLIsHiddenKey error:nil];
if (aBool && ![aBool boolValue]) {
[_iCloudURLs addObject:fileURL];
}
}
NSLog(@"Found iCloud URL: %@ .", _iCloudURLs);
NSLog(@"Found %d iCloud files.", _iCloudURLs.count);
_iCloudURLsReady = YES;
if ([self iCloudOn]) {
// Remove deleted files
// Iterate backwards because we need to remove items form the array
for (int i = _objects.count -1; i >= 0; --i) {
PTKEntry * entry = [_objects objectAtIndex:i];
if (![_iCloudURLs containsObject:entry.fileURL]) {
[self removeEntryWithURL:entry.fileURL];
}
}
// Add new files
for (NSURL * fileURL in _iCloudURLs) {
[self loadDocAtURL:fileURL];
}
self.navigationItem.rightBarButtonItem.enabled = YES;
}
if (_moveLocalToiCloud) {
_moveLocalToiCloud = NO;
[self localToiCloudImpl];
}
else if (_copyiCloudToLocal) {
_copyiCloudToLocal = NO;
[self iCloudToLocalImpl];
}
[_query enableUpdates];
}
間違ったコードを実行した場合にこの問題を解決するにはどうすればよいですか。正しい方法でアプローチするにはどうすればよいですか。