2013-05-01 64 views
0

我的應用程序使用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); 

... 

} 

回答

0

我不知道爲什麼你的目錄下有一個擴展名爲.db,我可能不理解你的quesion的全面影響,但是當我詢問在iCloud中存儲無處不在容器使用子目錄,我被告知一個我認爲是蘋果工程師的人可以添加子目錄,但是「沒有目錄應該有他們的名字,或者他們可能會被當作一個軟件包來處理,並且會傷心地爲你結束。你也不應該放置任何.nosync條目集裝箱的頂層「。他還說:「設置會將文檔顯示爲扁平的結構(例如,它只會顯示文件和包,而不是目錄),其他UI區域也可能在視覺上忽略子目錄。」

爲了從目錄的主表中選擇一個子目錄中的文件的詳細表,我使用了一種方法來遍歷查詢中的所有項目,並僅查找具有URL中子目錄的項目。