Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data with iCloud design

Im on iOS 7 and want to use core data with iCloud, this is working well, the issue I wanted some input on is the following.

Some of my Core Data objects have related images/large text files which I was storing as files on the file system and just setting the url of these items as string properties. Now obviously this wont just work with iCloud Core Data sync because iCloud has no notion of these files. I was wondering if you had any suggestions on how to handle this?

1) I could store the image data and large text files as core data properties directly but it does not seem to be the best way to go

2) I could use iCloud to also store the documents and then somehow try to keep sync between the files in different iOS devices, but this could potentially get messy.

3) Some other solution.

Any Suggestions?

Regards

Daniel

like image 217
Daniel Avatar asked Nov 08 '13 19:11

Daniel


People also ask

What is iOS Core Data?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.

Is Core Data still used?

Yes. It's a pretty old framework and its Objective-C roots are not very well hidden. Regardless, Apple's clearly still interested in having people use and adopt Core Data given the fact that they've added new features for Core Data + SwiftUI in iOS 15, and previous iOS versions received various Core Data updates too.

What is CloudKit Core Data?

Core Data owns the record ID for all of the objects that it creates in CloudKit. And, for each one, we will generate a simple UUID to use as its record name. When the Record Name is combined with a zone identifier you get a CKRecord ID. At the bottom, you'll see how Core Data manages type information.

Should I use CloudKit or firebase?

Rules can be set up in the dashboard: CloudKit has a lot of tabs. Summary: Firebase can be immediately accessed without authentication but also provides the flexibility to define your own rules. CloudKit is probably more secure and can provide a seamless experience for your users too, but only if they are using iOS.


2 Answers

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

like image 182
Fruity Geek Avatar answered Sep 27 '22 21:09

Fruity Geek


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.

like image 42
Warren Burton Avatar answered Sep 27 '22 22:09

Warren Burton