Search code examples
swiftcore-dataicloudcloudkit

Core data no icloud sync... again


Almost 2 years ago, I asked here (Core data and iCloud sync not working in live App) about a problem I had syncing core data on iCloud. The problem was resolved deploying the schema to production.

No problems so far, but some months ago I noticed my App some times was syncing, sometimes do not.

Nowadays, the problem is, not syncing at all, on the simulator works OK, I open 2 devices and they sync without problems.

Going live (downloading the App from the AppStore), using 2 iPhones, same iCloud account, does not sync 99% of the time.

For instance:

  1. delete the app from both iPhones
  2. download the apps from the AppStore
  3. the apps load the records already stored on iCloud, I can see same data on both iPhones
  4. add a new record on iPhone A, iPhone B receives the new data... ok
  5. add a second record on any iPhone and nothing, from here, none of the new data is synced
  6. To see if data on step 4 was saved on iCloud, the apps are deleted again from both phone and start over, on step 3, the data is loaded but the new record on step 4 is missing.

Any ideas?

AppDelegate.swift

lazy var persistentContainer: NSPersistentCloudKitContainer = {
    let container = NSPersistentCloudKitContainer(name: "whatever")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    container.viewContext.automaticallyMergesChangesFromParent = true
    container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
    return container
}()

func saveContext () {
    let context = persistentContainer.viewContext
    if context.hasChanges {
        do {
            try context.save()
        } catch {
            let nserror = error as NSError
            fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
        }
    }
}

Solution

  • Found the problem...

    I inadvertently created a second configuration in the datamodel, deleting it solved the problem, iCloud is syncing again without any problem.

    OLD CONFIG

    CORRECT CONFIG