1 Reply
      Latest reply on Dec 14, 2019 3:54 PM by andrewbuilder
      benfromchicago Level 1 Level 1 (0 points)

        I have a SwiftUI app sitting on top of a CoreData + CloudKit stack. If I have the app running on multiple devices, a change on one device will not always be reflected on the others. However, restarting the app on the other device will pick up the change. For example:


        Device A and Device B both have the app running. Device B has it in the background.


        Device A changes Record 1.

        Device B returns to the foreground, but does not see the change to Record 1

        Kill the app on Device B, relaunch, and it sees the change


        It seems that the cloudkit process isn't always getting change notifications from iCloud (note that this happens on actual devices, not just the sim).


        If we could tell the container "Hey, I just retuned to the foreground, maybe check to see if anything has changed?", that would, I think, fix the problem. I can't tear down my CoreData stack without rebuilding the entire app (since the main thread context is pushed down into SwiftUI).


        Is there a way to force this update?





        • Re: Force a NSPersistentCloudKitContainer sync
          andrewbuilder Level 1 Level 1 (0 points)

          Have you set automaticallyMergesChangesFromParent = true


          I choose to do this after I load my persistent container...


              lazy var persistentContainer: NSPersistentContainer = {

                  let container = NSPersistentCloudKitContainer(name: persistentStoreName)

                  container.viewContext.automaticallyMergesChangesFromParent = true

                  container.loadPersistentStores(completionHandler: { (storeDescription, error) in

                      if let error = error as NSError? {

                          fatalError("Unresolved error \(error), \(error.userInfo)")


                  container.viewContext.automaticallyMergesChangesFromParent = true

                  return container