2

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)?

Charlie S
  • 4,366
  • 6
  • 59
  • 97

2 Answers2

1

A good intro how to do this is given in the 2019 WWDC video „Using Core Data With CloudKit“.
The essential points are:

  • Replace NSPersistentContainer by ist subclass NSPersistentClouKitContainer.
  • Initialize the iCloud schema using the container function initializeCloudKitSchema (this has to be done only once after the core data model has been set up or changed).
  • In the iCloud Dashboard, make every custom type (these are the types starting with CD_) queryable.
  • In the iCloud Dashboard, set the security type of all CD_ record types for all authenticated users to read/write.
  • Implement history tracking for core data (here are Apple’s suggestions).

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:

    private (set) lazy var persistentContainer: NSPersistentCloudKitContainer! = {
        // This app uses 3 stores: 
        //  - A local store that is user-specific,
        //  - a private store that is synchronized with the iCloud private database, and
        //  - a shared store that is synchronized with the iCloud shared database.
        
        let persistentStoresLoadedLock = DispatchGroup.init() // Used to wait for loading the persistent stores
    
        // Configure local store
        // --------------------------------------------------------------------------------------------------
        let appDocumentsDirectory = try! FileManager.default.url(for: .documentDirectory, 
                                                                                                                         in: .userDomainMask, 
                                                                                                                         appropriateFor: nil, 
                                                                                                                         create: true)
        let coreDataLocalURL = appDocumentsDirectory.appendingPathComponent("CoreDataLocal.sqlite")
        let localStoreDescription = NSPersistentStoreDescription(url: coreDataLocalURL)
        localStoreDescription.configuration = localConfigurationName
        // --------------------------------------------------------------------------------------------------
    
        // Create a container that can load the private store as well as CloudKit-backed stores.
        let container = NSPersistentCloudKitContainer(name: appName)
        assert(container.persistentStoreDescriptions.count == 1, "###\(#function): Failed to retrieve a persistent store description.")
        let firstPersistentStoreDescription = container.persistentStoreDescriptions.first!
        let storeURL = firstPersistentStoreDescription.url!
        let storeURLwithoutLastPathComponent = storeURL.deletingLastPathComponent
        
        // Configure private store
        // --------------------------------------------------------------------------------------------------
        let privateStoreDescription = firstPersistentStoreDescription
        privateStoreDescription.configuration = privateConfigurationName
        // The options below have to be set before loadPersistentStores
        // Enable history tracking and remote notifications
        privateStoreDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
        privateStoreDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
        privateStoreDescription.cloudKitContainerOptions!.databaseScope = .private
        // --------------------------------------------------------------------------------------------------
    
        // Configure shared store
        // --------------------------------------------------------------------------------------------------
        let sharedStoreURL = storeURLwithoutLastPathComponent().appendingPathComponent("Shared")
        let sharedStoreDescription = NSPersistentStoreDescription(url: sharedStoreURL)
        sharedStoreDescription.configuration = sharedConfigurationName
        sharedStoreDescription.timeout                                                          = firstPersistentStoreDescription.timeout
        sharedStoreDescription.type                                                                 = firstPersistentStoreDescription.type
        sharedStoreDescription.isReadOnly                                                   = firstPersistentStoreDescription.isReadOnly
        sharedStoreDescription.shouldAddStoreAsynchronously                 = firstPersistentStoreDescription.shouldAddStoreAsynchronously
        sharedStoreDescription.shouldInferMappingModelAutomatically = firstPersistentStoreDescription.shouldInferMappingModelAutomatically
        sharedStoreDescription.shouldMigrateStoreAutomatically          = firstPersistentStoreDescription.shouldMigrateStoreAutomatically
        // The options below have to be set before loadPersistentStores
        // Enable history tracking and remote notifications
        sharedStoreDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
        sharedStoreDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
        sharedStoreDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions.init(containerIdentifier: "iCloud.com.zeh4soft.shop")
        // For sharing see https://developer.apple.com/documentation/cloudkit/shared_records
        // and https://medium.com/@adammillers/cksharing-step-by-step-33800c8950d2
        sharedStoreDescription.cloudKitContainerOptions!.databaseScope = .shared
        // --------------------------------------------------------------------------------------------------
    
        container.persistentStoreDescriptions = [localStoreDescription, privateStoreDescription, sharedStoreDescription]
        for _ in 1 ... container.persistentStoreDescriptions.count { persistentStoresLoadedLock.enter() }
    
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            // The completion handler will be called once for each persistent store that is created.
            guard error == nil else {
                /*
                Apple suggests to replace this implementation with code to handle the error appropriately.
                However, there is not really an option to handle it, see <https://stackoverflow.com/a/45801384/1987726>.
                Typical reasons for an error here include:
                * The parent directory does not exist, cannot be created, or disallows writing.
                * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                * The device is out of space.
                * The store could not be migrated to the current model version.
                Check the error message to determine what the actual problem was.
                */
                fatalError("###\(#function): Failed to load persistent stores: \(error!)")
            }
            if storeDescription.configuration == self.privateConfigurationName {
                /*
                Only if the schema has been changed, it has to be re-initialized.
                Due to an Apple bug, this can currently (iOS 13) only be done with a .private database!
                A re-initialization requires to run the app once using the scheme with the "-initSchema" argument.
                After schema init, ensure in the Dashboard:
                For every custom type, recordID and modTime must have queryable indexes.
                All CD record types must have read/write security type for authenticated users.
                Run later always a scheme without the "-initSchema" argument.
                */
                if ProcessInfo.processInfo.arguments.contains("-initSchema") {
                    do {
                        try container.initializeCloudKitSchema(options: .printSchema)
                    } catch {
                        print("-------------------- Could not initialize cloud kit schema --------------------")
                    }
                }
            }
            persistentStoresLoadedLock.leave() // Called for all stores
        })
        let waitResult = persistentStoresLoadedLock.wait(timeout: .now() + 100) // Wait for local, private and shared stores loaded
        if waitResult != .success { fatalError("Timeout while loading persistent stores") }
        return container
    }   ()  

EDIT:

privateConfigurationName, as well as sharedConfigurationName are Strings:

let privateConfigurationName = "Private"
let sharedConfigurationName  = "Shared"  

and Private and Shared are used as Configuration names in the Coredata model, e.g.:

enter image description here

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.

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
0

I did the following :

  • Replaced NSPersistentContainer by NSPersistentCloudKitContainer
  • And added enabled history tracking
container = NSPersistentCloudKitContainer(name: "myApp") // <<<<< this
if inMemory {
    container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
}
        
let description = container.persistentStoreDescriptions.first
description?.setOption(true as NSNumber, 
                       forKey: NSPersistentHistoryTrackingKey) // <<<<< this

container.viewContext.automaticallyMergesChangesFromParent = true
container.loadPersistentStores(...)

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...

LetsGoBrandon
  • 498
  • 8
  • 23