iCloud NSMetadata查询结果为空

时间:2013-02-19 16:06:25

标签: iphone icloud nsmetadataquery

嗨,朋友,我有NSMetaDataQuery结果是空白的,我分析了这个link。我很困惑。请帮助我,在这里我添加元查询的源代码

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

}

请帮助我如何解决这个问题,如果我做错了代码,请问我该如何处理正确的方法。

0 个答案:

没有答案