Ways to 'persist' CloudKit data

The 'Designing for CloudKit' section of the iCloud Design Guide states that CloudKit "is a service for moving data to and from iCloud and sharing data between users of your app. It’s your responsibility to convert model objects to records that you save using CloudKit, and to fetch changes made by other users and apply those changes to your model objects".


Many CloudKit tutorials that I have followed don't make use of model objects. This results in data should the internet become unavailable. What is the best way to model objects and their relationships that fetch from CloudKit?

I believe the 'model' in 'model objects' refers to the 'model' in 'MVC' (Model View Controller). If you are designing your app consistent with the MVC structure then you only need to store the objects that are relevant to your app's model.


You wrote:


>This results in data should the internet become unavailable.

I don't understand this. Perhaps you can restate. But if the Cloud is not available then the app doesn't synch. When the Cloud becomes available the app synchs if its last synch is still the current data on the Cloud. But if its last synch is not the current data on the Cloud then there can be a conflict and the app needs to resolve that conflict perhaps using input from the user ...


Wordy and clear:

"Warning - the data on the Cloud has been updated by another device since you last synched to it. Do you want to 1) synch to it and possibly lose changes you made on this device since your last synch or 2) overwrite it with the data in this device and possibly lose changes that another device made? 1) Synch 2) Overwrite 3) Cancel "

or

Concise and confusing:

"Possible synch erase. Synch/Overwrite/Cancel"

>I don't understand this. Perhaps you can restate.

A typo! When there is no internet available and the app is not loaded in memory, the data does not exist on the phone as there is no model for it to persist to! Currently, I am fetching CloudKit data and loading it straight in to a table view. I believe I need to have Swift classes representing each Record Type in CloudKit, and use subscriptions to check for changes in the data.


An example:


I have two record types: 'Specie' and 'Sighting'. In CloudKit, 'Sighting' has a 'specie' field of type Reference. This models the one-to-many relationship. I think I need to create a Specie.swift and a Sighting.swift class and fetch the CloudKit data from within these classes. Then, whenever there is no internet connection, the table view is still able to fetch the data persisted to my model. Specie records are read only so there would never be synch issues.


Thanks!


Edit: This extract from tuts+ Code sums up my point quite well:


CloudKit doesn't offer local storage, for example. This means that an application running on a device without a network connection is pretty much useless if it solely relies on CloudKit.

You use the cloud to synch your data between devices and big events (like deleting and reinstalling your app) not to 'persist' the data. Each device keeps a version of its data locally. But it synchs what it thinks is the current data with the cloud. If the cloud data is newer than what it has then it accepts those changes. If it can't find the cloud it operates using its local data. The next time it synchs with the cloud it has to check to deal with any conflicts in overlapping data (i.e. if both the cloud data and also the local data changed since the last time the device synched then there could be a conflict - you keep a record of the lastTimeSynched locally and the lastTimeSynched on the cloud. If they differ and if you changed the data locally since lastTimeSynched then you have a possible conflict).

Ways to 'persist' CloudKit data
 
 
Q