Force a NSPersistentCloudKitContainer sync

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?


Thanks!


Ben

Replies

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

}()

Ben did you ever figure this out? I'm running into a similar issue (and yes, I did set automaticallyMergesChangesFromParent to true.

I also am seeing some odd behavior where CloudKit just stops responding all together. I'm still trying to get to the bottom of what's going on. So far I've examined my device's logs when the issue is occurring and there are no CloudKit+CoreData entries in the log (there are normally tons when everything is working as expected). It seems to happen when I upgrade the app (via TestFlight) but I still haven't confirmed that. I can sometimes fix this by logging out and back into iCloud on my device, which is a huge pain. I don't think it's anything in my code, so curious as to if anyone else has run into this.

To your point in the original question, would be nice to force this update and get some sort of error if it fails (so I at least know the syncing is having issues without hooking my device up to Xcode every time to verify the log messages are there)
  • Hey Derrick,

    Builds distributed via TestFlight likely uses production environment, and local tethered builds use development environment.

    Did you upload the schema to CloudKit and deploy that into production?

Add a Comment
Derrick your comment saved my sanity. I also experience the behavior you described. My app stops syncing with iCloud after my app is updated via TestFlight. Signing out of my profile on the device and signing back in fixed the issue.
  • Has anyone found a solution to this other than the comment described by @kesterDL? I just ran into this yesterday where a TestFlight update caused the syncing to stop and it makes QA (and me) nervous to ship when we see this behavior.

Add a Comment

I'm seeing this issue as well. Interesting that it might be TestFlight causing it. Attempting to sign out to see if it works..