简体   繁体   中英

How do I enable iCloud and migrate Core Data on a release update?

I have a released V1.0 of an iPhone App which uses Core Data but no iCloud. Now I will like to release V2.0 with iCloud support.

I am using https://github.com/mluisbrown/iCloudCoreDataStack/blob/master/iCloudCoreDataStack/PersistentStack.m as a template, and everything seems to work.

Because the previous release did not have iCloud, and the new one will, I will like to migrate the local copy to the iCloud one on the first time the user opens the new version.

How do I do that? Can I simply somehow copy the whole sqlite file from local to iCloud? The schema is the same between 1.0 and 2.0.

I plan to ask the user if they want to use local Vs. iCloud (as per guidelines), but if they say Yes, I want to just do a bulk copy somehow.

Thanks

You can't bulk copy, you have to use proper API [NSPersistentStoreCoordinator migratePersistentStore:...] and [NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:...] with iCloud options to migrate the store to iCloud. I have attempted to document how to do this (but the example uses UIManagedDocument, although the Core Data APIs for migration to and from iCloud remain the same).

Not only do you have to check the user preferences you also have to check whether they are logged in to iCloud. Also you have to migrate to iCloud and then you have to be able to migrate back to Local if the user changes the preference. So you have to check whether the user has changed settings whenever the app becomes active. It is not that straight forward to do but it's not to hard once you figure out the logic required.

See the link below for a more detailed explanation, sample code, and a video showing the app working and the creation/removal of files in the local and iCloud containers:

http://ossh.com.au/design-and-technology/software-development/uimanageddocument-icloud-integration/

Remember you will have to remove the UIManagedDocument specific code from the example.

I have just posted a sample iOS library style Core Data app which includes iCloud integration. The app includes a Settings Bundle for the user to toggle "Use iCloud" preference settings and will migrate the store to and from iCloud depending on the users settings.

Download from the link below - sorry about the documentation - will get around to that at some point but it works much the same way as the UIManagedDocument example.

http://ossh.com.au/design-and-technology/software-development/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM