Core Data with iCloud design

浪尽此生 提交于 2019-12-03 15:29:49

If you are setting up the model programmatically, add a message to setAllowsExternalBinaryDataStorage: to allow Core Data to decide when to save it as a record or separate file automatically. Core Data manages the external files for you when it decides to use them, so it should "just work" in iCloud as well.

NSAttributeDescription * imageAttribute;
//Initialise, setup, add to model, etc
[imageAttribute setAttributeType: NSBinaryDataAttributeType];
[imageAttribute setAllowsExternalBinaryDataStorage:YES];

There is a corresponding checkbox for the attribute in the model editor UI as well.

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSAttributeDescription_Class/reference.html#//apple_ref/doc/uid/TP30001175-SW26

Place the files in your iCloud document sandbox also and they will keep synced across all your devices.

You can use a relative reference to the items as they will appear in the file system at the same relative point.

<ubiquitous container URL>/Documents/something.jpg

A minor issue is that the sync isn't seamless and you will have to explicitly request the item to be downloaded from iCloud if it isn't on device already.

So at its most primitive...

NSDictionary *dict = [urlToIcloudResource resourceValuesForKeys:@[NSURLUbiquitousItemIsDownloadedKey] error:&error];

NSNumber *isDownloaded = dict[NSURLUbiquitousItemIsDownloadedKey];

if (![isDownloaded boolValue]) {
   BOOL res = [[NSFileManager defaultManager] startDownloadingUbiquitousItemAtURL:urlToIcloudResource error:&error];
}

So you can keep using your current strategy of dumping them in the file system and just using a string reference to them. iCloud will keep them synced.

The suitability of this solution does depend on whether those large blobs are write-once-read-many objects (very suitable) or read-write-often type objects (trickier).

One advantage is that for text objects like txt or json if you keep the extension on the file iCloud should "just work" for keeping multiple device edits in sync.

Raw data objects like jpg or pdf will be last edit wins.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!