我一直在努力將icloud添加到我的項目中(這在包子中很痛苦),我可以保存和移除文件,但是我無法獲得存儲在iCloud中的文件。我試過了大約10個不同網站的解決方案(包括Apple文檔)。每當我打電話給[self.query startQuery];
,一切似乎都在起作用:調用正確的方法,這些方法的執行完全按照它們的方式執行。然後,當我要求我的應用的iCloud Documents目錄中的文件有nsarray時,我得到兩個圓括號(當我在NSLog中查看時):File List: ()
。我知道在我的應用程序的iCloud Documents目錄中有很多不同的文檔,包括所有形狀,擴展名,大小和名稱,因爲我一直在使用iCloud Developer網站來檢查是否正常工作。我的設置第一種方法查詢如下:iCloud NSMetadata查詢結果爲空
- (void)syncWithCloud {
self.query = [[NSMetadataQuery alloc] init];
NSURL *mobileDocumentsDirectoryURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
[query setSearchScopes:[NSArray arrayWithObjects:NSMetadataQueryUbiquitousDocumentsScope, nil]];
//[query setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE '*'", NSMetadataItemFSNameKey]];
[query setPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%%K like \"%@*\"", [mobileDocumentsDirectoryURL path]], NSMetadataItemPathKey]];
//Pull a list of all the Documents in The Cloud
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(processFiles:)
name:NSMetadataQueryDidFinishGatheringNotification object:self.query];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(processFiles:)
name:NSMetadataQueryDidUpdateNotification object:self.query];
[self.query startQuery];
}
由蘋果公司提供的process files method是下一個:
- (void)processFiles:(NSNotification*)aNotification {
NSMutableArray *discoveredFiles = [NSMutableArray array];
//Always disable updates while processing results.
[query disableUpdates];
//The query reports all files found, every time.
NSArray *queryResults = [query results];
for (NSMetadataItem *result in queryResults) {
NSURL *fileURL = [result valueForAttribute:NSMetadataItemURLKey];
NSNumber *aBool = nil;
// Don't include hidden files.
[fileURL getResourceValue:&aBool forKey:NSURLIsHiddenKey error:nil];
if (aBool && ![aBool boolValue])
[discoveredFiles addObject:fileURL];
}
//Update the list of documents.
[FileList removeAllObjects];
[FileList addObjectsFromArray:discoveredFiles];
//[self.tableView reloadData];
//Reenable query updates.
[query enableUpdates];
NSLog(@"File List: %@", FileList);
}
爲什麼沒有這個給我的文件列表,或至少某種數據?我是否定義了NSMetadata查詢錯誤,也許我的謂詞沒有格式化?我知道我做錯了什麼,因爲沒有辦法讓iCloud變得這麼複雜(或者可以嗎?)。
感謝您的幫助!
編輯#1:我正在繼續嘗試不同的方法來解決這個問題。使用其中一個答案下面我已經改變了謂詞濾波器如下:
[query setPredicate:[NSPredicate predicateWithFormat:@"NSMetadataItemFSNameKey LIKE '*'"]];
我加以下行之前[query enableUpdates]
呼叫:
for (NSMetadataItem *item in query.results) {
[FileList addObject:[item valueForAttribute:NSMetadataItemFSNameKey]];
}
在processFiles
方法,我已經嘗試將所有代碼放在後臺線程上,但這沒什麼區別 - 事實上,當代碼不在後臺線程上執行時FileList
會給我這個(null)
而不是這個()
。
難道我的問題與線程管理或內存分配有關嗎?請注意,我正在使用ARC。
編輯#2:FileList
變量是一個NSMutableArray在我@interface
定義並在-(id)init
方法初始化調用processFiles
方法之前。
編輯#3:當用斷點測試我的代碼時,我發現下面的for-loop永遠不會被貫穿 - 甚至一次都沒有。這使我相信:
A.正確的目錄未與
B. iCloud的連接無法看到該目錄中的文件
C.我NSMetadataQuery沒有被正確安裝 D.完全不同的東西
這裏是一個開始永遠也不會被運行for循環的代碼:
NSArray *queryResults = [query results];
for (NSMetadataItem *result in queryResults) {
我還沒有得到任何結果。我甚至嘗試將流程從背景隊列中移出,但是所做的只是將結果設爲'(null'而不是'()'。是否有可能我的'URLForUbiquityContainerIdentifier'設置不正確? –
You不需要上面的NSURL,因此你不需要ContainerIdentifier。看看[示例代碼](https://github.com/erica/iOS-5-Cookbook/tree/master/C18/02- UIDocument)從Erica Sadun精美的iOS食譜中找到,你需要檢查CloudHelper類 – SAE
究竟是什麼FileList?從命名約定看起來像一個類,不像對象或變量。 – SAE