I have an app that uses local device only CoreData (NSPersistentContainer). I am looking to migrate so the app is compatible with NSPersistentCloudKitContainer. I understand all the CloudKit setup for NSPersistentCloudKitContainer, but how do I migrate the data that is on the player’s phones to iCloud? (i.e. how do I migrate existing core data from NSPersistentContainer to NSPersistentCloudKitContainer)?
Question posted in Xcode
Whether you're new to Xcode or an experienced developer, our archive has everything you need to know about this integrated development environment (IDE). From basic functionalities to advanced features, our archive covers a wide range of Xcode-related questions and answers. Browse our archive now and find solutions to your Xcode questions, and take your app development skills to the next level
Whether you're new to Xcode or an experienced developer, our archive has everything you need to know about this integrated development environment (IDE). From basic functionalities to advanced features, our archive covers a wide range of Xcode-related questions and answers. Browse our archive now and find solutions to your Xcode questions, and take your app development skills to the next level
2
Answers
A good intro how to do this is given in the 2019 WWDC video „Using Core Data With CloudKit“.
The essential points are:
NSPersistentContainer
by ist subclassNSPersistentClouKitContainer
.initializeCloudKitSchema
(this has to be done only once after the core data model has been set up or changed).In case you have multiple persistent stores (e.g. a local store relevant only to one device, a private store shared with all users with the same Apple ID, and a shared store shared with other users), one way to set up this is the following:
EDIT:
privateConfigurationName
, as well assharedConfigurationName
areStrings
:and
Private
andShared
are used as Configuration names in the Coredata model, e.g.:One has to assign there the entities to the persistent store(s).
A warning:
I you assign the same entity to multiple persistent stores, a save of a managed context will store it in all assigned stores, except you assign a specific store, see this post.
Likewise, a fetch will fetch a record from all persistent stores the entity is assigned to, except you set
affectedStores
in the fetch request, see the docs.I did the following :
NSPersistentContainer
byNSPersistentCloudKitContainer
EDIT: Well, I talked too quick. It does not work 🙁
I found that people have little tricks here https://developer.apple.com/forums/thread/120328
(for example edit the items to trigger a sync, or to manually transfer each object as said explained here https://medium.com/@dmitrydeplov/coredata-cloudkit-integration-for-a-live-app-57b6cfda84ad)
But there is no actual answers…