EverBox开发笔记-3-iCloud Document Storage
原想將數據存到SQLite數據庫中(沙盒中的文件),再將SQLite數據庫文件上傳到iCloud,于是需要學習iCloud存儲文件對象的方法。
一,能直接將SQLite文件保存到iCloud嗎?
答案是否定的!不能直接將SQLite文件保存到iCloud中,原因是:
Accessing live database files in iCloud using the SQLite interfaces is not supported and will likely corrupt your database.
在這句話的下面,Apple再次提醒開發者是否真的需要SQLite來存儲數據:
SQLite stores are intended for apps that have large amounts of data to manage or want fine-grained change notifications. You do not need to read these sections if you are creating an atomic binary store.
于是就引出了Atomic Binary Store。什么是Atomic Binary Store?其實就是二進制文件,整體作為數據倉庫存在。跟iCloud結合起來描述:當文件某一部分發生變化時,文件的整體(而不像SQLite那樣,只有修改的內容)會被傳輸到iCloud。
?
二,在iCloud中如何存儲獨立文件(比如音頻) 除了在iCloud Storage這一章中對Document Storage進行描述外,貼心的蘋果又給我們提供教程了——Your Third iOS App 不過這個教程似乎并不對我的胃口,我只想把沙盒中的文件存到iCloud,需要時下載下來,不需要時就刪除,不用修改文件,但所謂見微知著,在照貓畫虎把教程中的內容重現一遍后,在這個過程中讀到的知識幫我解決了這個問題, 方法是: 1,獲取沙盒中文件的位置,得到一個NSURL類型指針。測試時,我在沙盒內創建了一個文件 _createdFilePath: NSFileManager * fm = [NSFileManager defaultManager]; NSArray * paths = [fm URLsForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask]; NSURL * documentURL = [paths objectAtIndex:0]; _createdFilePath = [documentURL URLByAppendingPathComponent:@”localfile.dat” isDirectory:NO]; 2,在iCloud中創建一個路徑,用于保存文件。 NSString * string = [NSString stringWithFormat:@”Created at: %@”, [[NSDate date] description]]; NSFileManager * fm = [NSFileManager defaultManager]; NSURL * newDocumentURL = [fm URLForUbiquityContainerIdentifier:nil]; newDocumentURL = [newDocumentURL URLByAppendingPathComponent:@”directory_in_iCloud” isDirectory:YES]; newDocumentURL = [newDocumentURL URLByAppendingPathComponent:@”iCloudFile.dat”]; 3,將沙盒中的文件保存到iCloud中。 [fm setUbiquitous:YES itemAtURL:_createdFilePath destinationURL:newDocumentURL error:nil]; ? 第2步和第3步不能在主線程中執行,因為iCloud相關操作可能耗時較長,為避免阻塞主線程,可以使用dispatch queue: dispatch_async(dispatch_get_global_queue(DISPATCH_QUUE_PRIORITY_DEFAULT), 0), ^{ 2的代碼。 3的代碼。}); ? 如果需要對文件的內容進行修改,比如保存字符串對象的文件,你就需要子類化(subclass)一個UIDocument類,并重載其中的兩個接口: 讀數據接口: loadFromContents: ofType: error: 寫數據接口: contentsForType: error: 對于這種需求Your Third iOS App 是個極好的例子。轉載于:https://www.cnblogs.com/tara/archive/2012/06/29/2564149.html
總結
以上是生活随笔為你收集整理的EverBox开发笔记-3-iCloud Document Storage的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全国计算机二级c语言考什么,国家计算机二
- 下一篇: 量子纠缠的机制是什么?一定要理解整体性概