2014-01-13 16 views
0

我知道文件存在,因爲我可以下載它,但我需要檢查它是否存在。我曾嘗試使用如何在iCloud中驗證文件的存在?

[NSFileManager contentsOfDirectoryAtPath:error:] 

但它給了null。我不明白爲什麼這是因爲我仍然可以下載我正在尋找的文件。也許這是一個不正確的URL,但我使用的URL是我在創建我正在尋找的UIDocument時打印的那個URL。也許我使用了錯誤的方法?

編輯:

我一直在使用NSMetadataQuery也嘗試過了,我可以得到它來回饋通知,但它並沒有曾經有,即使我可以明確地下載我要找的文件的結果。

回答

3

要查找iCloud中的文件,請使用NSMetadataQuery。這將找到已下載的文件以及用戶帳戶中尚未下載到本地設備的文件。最好使用NSFileManager只會查找已經下載的文件。

你設置了這樣的事情:

NSMetadataQuery *query = [[NSMetadataQuery alloc] init]; 
[self setMetadataQuery:query]; 
[query setSearchScopes:@[NSMetadataQueryUbiquitousDataScope]]; 
[query setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE '*'", NSMetadataItemFSNameKey]]; 

你要觀察NSMetadataQueryDidStartGatheringNotificationNSMetadataQueryDidUpdateNotification,大概NSMetadataQueryDidFinishGatheringNotification。然後開始查詢:

[query startQuery]; 

完成後,您將在查詢發現iCloud文件時收到通知。該通知將包括NSMetadataItem情況下,你可以用它來得到這樣的文件大小,下載狀態信息等

+0

謝謝,但是這不是爲我工作無論是。我問這個問題的原因是因爲我不能讓NSMetadataQuery工作,我想確保我要找的文件是在我認爲他們是(我記錄我UIDocument的指定fileurl)的地方。 – user1654889

+0

'NSMetadatqQuery'是你如何做到的。如果你無法使用它,請發佈一個關於使其工作的問題。這就是API,如果你使用正確,但沒有得到任何結果,(a)文件不在那裏,或(b)iCloud被搞砸了,你無法修復。 –

1

使用元數據查詢 - 這裏是一些示例代碼

/*! Creates and starts a metadata query for iCloud files 

*/ 
- (void)createFileQuery { 
    [_query stopQuery]; 

     if (_query) { 
      [_query startQuery]; 
     } 
     else { 
      _query = [[NSMetadataQuery alloc] init]; 

      [_query setSearchScopes:[NSArray arrayWithObjects:NSMetadataQueryUbiquitousDocumentsScope, NSMetadataQueryUbiquitousDataScope, nil]]; 
      // NSString * str = [NSString stringWithFormat:@"*.%@",_fileExtension]; 
      NSString *str = @"*"; 
      [_query setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE %@", NSMetadataItemFSNameKey, str]]; 

      NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter]; 
      [notificationCenter addObserver:self selector:@selector(fileListReceived) name:NSMetadataQueryDidFinishGatheringNotification object:_query]; 
      [notificationCenter addObserver:self selector:@selector(fileListReceived) name:NSMetadataQueryDidUpdateNotification object:_query]; 
      [_query startQuery]; 
     } 

} 

/*! Gets called by the metadata query any time files change. We need to be able to flag files that 
we have created so as to not think it has been deleted from iCloud. 

*/ 
- (void)fileListReceived { 
    LOG(@"fileListReceived called."); 

    NSArray* results = [[_query results] sortedArrayUsingComparator:^(NSMetadataItem* firstObject, NSMetadataItem* secondObject) { 
     NSString* firstFileName = [firstObject valueForAttribute:NSMetadataItemFSNameKey]; 
     NSString* secondFileName = [secondObject valueForAttribute:NSMetadataItemFSNameKey]; 
     NSComparisonResult result = [firstFileName.pathExtension compare:secondFileName.pathExtension]; 
     return result == NSOrderedSame ? [firstFileName compare:secondFileName] : result; 
    }]; 

    //FLOG(@" results of query are %@", results); 
    for (NSMetadataItem* result in results) { 
     NSURL* fileURL = [result valueForAttribute:NSMetadataItemURLKey]; 
     NSString* fileName = [result valueForAttribute:NSMetadataItemDisplayNameKey]; 
     NSNumber* percentDownloaded = [result valueForAttribute:NSMetadataUbiquitousItemPercentDownloadedKey]; 
     NSNumber *isDownloaded = nil; 
     NSNumber *isDownloading = nil; 
     NSError *er; 
     [fileURL getResourceValue: &isDownloaded forKey:NSURLUbiquitousItemIsDownloadedKey error:&er]; 
     [fileURL getResourceValue: &isDownloading forKey:NSURLUbiquitousItemIsDownloadingKey error:&er]; 
     FLOG(@" Found file %@", fileName); 
    } 

} 
+0

正是我需要的。 –

1

docs

在iOS中,需要時主動下載文件。 iCloud中的項目,但不是 ,但本地不是由iOS自動下載;只會自動下載其元數據 。首次下載新的基於iCloud的文檔需要您在應用中注意並仔細設計。 在您明確下載此類項目後,系統會處理 下載從iCloud到達的更改。

考慮跟蹤 文件下載狀態,作爲iOS應用程序模型圖層的一部分。擁有 這個信息可以讓你提供更好的用戶體驗:你可以設計你的應用,當他們想要 打開一個還不是本地的文檔時,不會讓用戶長時間延遲。對於您應用的元數據查詢所提供的每個文件(或文件 包)URL,請通過調用NSURL方法 getResourceValue:forKey:error:獲取值 NSURLUbiquitousItemIsDownloadedKey密鑰。讀取未下載文件 可能需要很長時間,因爲協調讀取 操作會阻止,直到文件完成下載(或未能下載 )。

對於尚未本地的文件(或文件包),您可以在用戶請求時或之前啓動 下載。如果您的應用程序的用戶 文件不在數量大或很大,要考慮 一個策略是主動下載您的元數據 查詢指定的所有文件。用於由查詢提供的每個文件(或文件包)的URL,通過調用方法NSFileManager startDownloadingUbiquitousItemAtURL:error:使 相應項目本地。如果你通過這種方法 網址爲已本地項目,該方法不執行任何工作和 返回YES。

更新:iOS7應該使用NSURLUbiquitousItemDownloadingStatusKey而不是NSURLUbiquitousItemIsDownloadedKey

相關問題