Core Data and iCloud Migration Scenarios
Core Data and iCloud Migration Question:
“Am I right in thinking that I need to implement the following:
– check if iCloud is available
– if available then invite user to use iCloud storage
– either use Local or iCloud storage (save preference)
– register for notifications should iCloud toggle change in settings
– implement mirgration of Local to iCloud or iCloud to Local
– delete storage not used”
– Check if iCloud is available (i.e. user has signed in)
– Check if user has previously set a preference for using iCloud or local storage for the app
– If iCloud is available and preference is set to use iCloud then check if iCloud store exists and open it. If it does not exist then better decide on some corrective action – i.e. check if local store exists, recover from a backup, open a new file, and importantly figure out how you got into this situation …
– If iCloud is available and preference has not been set then prompt user whether to use local or iCloud storage. Create store with or without iCloud options (Note: store is always created on local storage, however, my approach is to use a different file name if iCloud options are used to make it easy to determine whether the file has logs replicated via iCloud and whether the app should open the file with iCloud options). Additional checks if iCloud option is selected:
– Check if a local store exists (should never get here because preference has not been set yet, but you never know), if so then ask the user what to do with it (delete it or migrate it). Before migrating it you should check if one already exists in iCloud (created by another device) and take some action – merge data, override data, use iCloud data.
– Also remember the user may have created a file on another device and shared it via iCloud, in which case you don’t want to do anything other than create the database file and sync the data from iCloud. This is only really a problem if your app loads seed data.
– If iCloud is selected and the network is not available (e.g. airplane mode) and the device has not yet had a chance to download iCloud files its not possible to determine whether a file already exists in iCloud – User Manual to explain these scenarios…
– If no iCloud file exists then create the file with iCloud options and load required seed data, always a good idea to ask the user to confirm this is the first time to prevent duplicate seed data from being created.
– If local storage is selected then check if a local store already exists and open it, otherwise create a new one and load any required seed data.
– If iCloud is available and preference is set to local storage then check if iCloud availability has changed (i.e. suddenly become available or new iCloud account being used) and prompt user whether to use iCloud. If iCloud is selected then check for an existing local file and prompt user whether to migrate the existing file and merge iCloud data or override iCloud data or whether to use the iCloud data only (replace existing local data).
How to Stop Using iCloud ?
– If user disables iCloud, or logs out of iCloud then in theory you no longer have access to the iCloud store so the app should prompt the user to turn iCloud back on and first migrate their store to a local store before turning iCloud off.
– In order to give the user the option of reverting to local storage you need to provide an app setting to do this. This can be done either in the Settings app using a Settings bundle or you can create your own custom settings page for the app.
– When the user selects this option you simply migrate the iCloud store to a local file and optionally remove the iCloud copy. Doing so will remove it from other devices as well so your app needs to be able to detect this removal and take the required action.