【发布时间】:2013-05-01 20:45:03
【问题描述】:
我的应用程序正在使用 UIDocument + iCloud 创建和查看文档。文件以这种方式存储:
- 文档/文件名.db
- Documents/Filename.db/File.db(Filename.db 是目录)
Documents/Filename.db 目录中的文件可以在其自己的视图控制器中正常加载。在另一个视图控制器中打开 Documents/Filename.db/File.db 时,我在加载这些文件时遇到问题。 文件名不是固定的,而且会多次出现。我可以验证文件是否存储在目录中,所以这应该不是问题。
我想我已经找到了 NSMetadataQuery 的问题,因为它只在 Documents/ 中搜索 - 我希望它在 Documents/Filename.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);
...
}
【问题讨论】:
标签: iphone ios objective-c icloud uidocument