2012-12-02 43 views
0

我是iCloud存儲的相對初學者,在構建我的下一個應用程序之前,我想就什麼更好的問題獲得一些意見。iCloud:KeyValue存儲或文檔存儲?

這個應用程序將需要存儲包含字符串數組的其他許多NSDictionaries。將會有365個字典(每年一個)每個至少有8個包含小字符串的數組。我知道這個數據類型可以存儲在鍵值中,但是我沒有經驗來判斷它是否會超過1mb的限制。

所以我的問題是,對於上面描述的情況,我應該在icloud上使用鍵值還是文檔存儲?

謝謝。

回答

2

如果可能超過最大限值時,你應該考慮存儲在PLIST你的字典文件或核心數據。

假設您想使用PLIST文件。您可以將字典寫入保存在文檔目錄中的文件。然後,使用下面的代碼,您可以將PLIST文件從文檔目錄移至iCloud,並將其同步到其他設備。

當您的應用程序檢測到iCloud中存在PLIST文件的更新版本(您可以在iCloud中檢查文件的修改日期並查看它是否比本地存儲的文件更新時),將其從iCloud中複製並放置它在文檔目錄中。

//find the URL of your app's ubiquitous container 
//setting URLForUbiquityContainerIdentifier to nil returns the URL for the first ubiquitous container in the list in your app's entitlements, you can replace nil with a string 
self.ubiquitousURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]; 

//you may want to update self.ubiquitousURL to the documents folder in the ubiquitous container 
//self.ubiquitousURL = [self.ubiquitousURL URLByAppendingPathComponent:@"Documents"]; 

//place the PLIST in iCloud 
[[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:plistURL destinationURL:[self.ubiquitousURL URLByAppendingPathComponent:@"file.plist"] error:NULL]; 

//you have detected there is a new file in iCloud and want to copy it to the documents directory 
[[NSFileManager defaultManager] startDownloadingUbiquitousItemAtURL:[self.ubiquitousURL URLByAppendingPathComponent:@"file.plist"] error:NULL]; 
[[NSFileManager defaultManager] copyItemAtURL:[self.ubiquitousURL URLByAppendingPathComponent:@"file.plist"] toURL:plistURL error:NULL]; 

self.ubiquitousURL是您的iCloud目錄的URL。 plistURL是文檔目錄中PLIST文件的URL。

+0

傑克,謝謝這真的是一個有用的答案。當前瀏覽iCloud文檔設計指南。 –

0

此處有很多選項。您可以使用應用程序的Documents目錄來存儲PLIST,SQLite數據庫,或者更好地使用Core Data。您實際上可以使用其中的任何一種,並且稍後還會集成iCloud。無論您是否使用iCloud,上面的建議都是從最壞的到最好的。 PList肯定是最簡單的。

如果您想了解更多關於核心數據和iCloud中,我強烈建議斯坦福大學的CS193P當然,提供免費的iTunes U.