我的應用程序使用UIDocument + iCloud創建和查看文檔。這些文件存儲這樣:使用NSMetadataQuery在目錄中搜索文件
- 文件/文件名.db
- 文件/文件名.db/File.db(文件名.db是目錄)
的文件/文件名中的文件.db目錄在其自己的View Controller中正常加載。在另一個View Controller中打開Documents/Filename.db/File.db時,我在加載這些文件時遇到問題。 文件名不固定,並且有多次出現。我可以驗證文件是否存儲在目錄中,所以不應該是問題。
我想我已經找到了問題所在,以NSMetadataQuery,因爲它只是裏面的文件/搜索 - 我希望它的目錄文件/文件名.db內搜索。我已經嘗試將查詢serachScope設置爲正確的路徑,但它似乎需要使用NSMetadataQueryUbiquityContainerDocumentsScope進行搜索以返回結果。
- (NSMetadataQuery *)documentQuery
{
NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
NSString *appending = [NSString stringWithFormat:@"Documents/%@.db/", currentDirectory]; // Directory name
NSURL *documentPath = [[[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil] URLByAppendingPathComponent:appending];
NSLog(@"Document Path: %@", documentPath);
if (query) {
[query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
NSString *filePattern = [NSString stringWithFormat:@"*.%@", STACK_EXTENSION];
[query setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE %@", NSMetadataItemFSNameKey, filePattern]];
}
return query;
}
- (void)processiCloudFiles:(NSNotification *)notification
{
[_query disableUpdates];
[_iCloudURLs removeAllObjects];
NSArray *queryResults = [_query results];
for (NSMetadataItem *result in queryResults) {
NSURL *fileURL = [result valueForAttribute:NSMetadataItemURLKey];
NSLog(@"---- FileURL: %@", fileURL);
NSNumber *aBool = nil;
[fileURL getResourceValue:&aBool forKey:NSURLIsHiddenKey error:nil];
if (aBool && ![aBool boolValue]) {
NSLog(@"Adding following Stack: %@", fileURL);
[_iCloudURLs addObject:fileURL];
}
}
NSLog(@"Found %d iCloud files", _iCloudURLs.count);
NSLog(@"iCloud URLs: %@", _iCloudURLs);
...
}