Questions About Updating Older Core Data / iCloud App to Core Data / CloudKit

I have an older Core Data / iCloud app that I wish to convert toi Core Data / CloudKit. I also want to do a major update to iOS 13.0


My questions are about the persistent store that is in 'ubiquity'.


In my app, the original path to the persistent store is

Original iCloud store path: /var/mobile/Containers/Data/Application/Blah1/Documents/iCloudStore.sqlite


However, I have to add the options for the persistent store coordinatoor as follows:

iCloudOptions = [NSDictionary dictionaryWithObjectsAndKeys:

@"iCloudStore", NSPersistentStoreUbiquitousContentNameKey, nil];


The path to the actual persistent store in upbiquity is now

Ubiquity iCloud store path: /var/mobile/Containers/Data/Application/Blah1/Documents/CoreDataUbiquitySupport/mobile~Blah2/iCloudStore/Blah3/store/iCloudStore.sqlite


NSPersistentStoreUbiquitousContentNameKey is deprecated.


The question is, can I just grab the persistent store that is in the Original location, or do I have to grab the persistent store that is in the Ubiquity location?


Also, if I need to get the Ubiquity store, can. I use NSPersistentStoreUbiquitousContentNameKey with a deployment target of 13.0?


Also, how do I delete the old iCloud data (or can I) for the user when the app starts using CloudKit?

Replies

Once I have the path, I plan to do this.


lazy var persistentContainer: NSPersistentCloudKitContainer = {

let container = NSPersistentCloudKitContainer(name: “MyAppName”)

// Create a store description for a CloudKit-backed local store

let cloudStoreLocation = URL(fileURLWithPath: Original or Ubiquity Path)

let cloudStoreDescription =

NSPersistentStoreDescription(url: cloudStoreLocation)

cloudStoreDescription.configuration = "Cloud"


// Set the container options on the cloud store

cloudStoreDescription.cloudKitContainerOptions =

NSPersistentCloudKitContainerOptions(

containerIdentifier: "com.my.container")

// Update the container's list of store descriptions

container.persistentStoreDescriptions = [

cloudStoreDescription,

]

// Load store

container.loadPersistentStores { storeDescription, error in

guard error == nil else {

fatalError("Could not load persistent stores. \(error!)")

}

}

return container

}()

Although, I probably won't use a configuration as it's all going to sync.

Do not attempt to reuse the ubiquity store file. The only possible migration path is a heavy weight migration, that is manually moving objects from the ubiquity store to a new store file to use with CloudKit.


You also need to be aware that in doing this, you are forever leaving behind clients on mixed versions of your app unless you plan to support bi-directional sync between the ubiquity and cloudkit stores yourself.

Actually, I've come up with another scheme. I have created Decodable structs for each core data entity. The new core data's store file location is in the Library folder whereas the old core data's store file is located in the Documents directory. I plan on saving every entity in structs and then go from structs to core data using two managed object contexts, one to grab the data and the other to save. This will allow that data to sync.
My Codable struct approach worked very well. I am very happy with it.