I have a simple shopping list app that is using Core Data with Cloudkit. A user creates a list then adds items to the list. The list can be shared to other users. Core Data is first used to save the Lists and Items as NSManaged Objects. A list is then shared to another user who can add to items to the list, and both users working on the same list stay in sync with each other. This is all mostly working but I am having some trouble getting Core Data to update the locally stored NSManaged objects with changes from the Cloud Kit Share.
Currently the app is dealing with a mix of NSManaged Objects and CKRecords to display in the table View that shows the items on a list. Which is a bit messy. I really want to just work with NSManaged Objects with Cloudkit doing the work of keeping things in sync between the 2 users.
When User B adds an Item it successfully saves to the Shared Database and sets the parent as the list that was shared. This is where I get a bit stuck as I want User A to see the Item that User B added and I want to use the NSManaged object that CloudKit makes in the background to update the UI.
In the background Cloudkit sees the new CKRecord that User B has added onto the shared database and makes a new NSManaged Object for User A. I can only get Core Data to save the new NSManaged Object Cloudkit makes when the app goes to the background or is closed then restarted. I do wait around a minute to make sure the sync happens. Checking the Sqlite database for user A the new Item added by User B isn't in there. When the app goes to the background and I check the Sqlite database again the Item is then added. It looks like the NSManaged object that Cloudkit makes is only saved to Core Data when the app goes to the background.
Long story short, Is there a way to get Core Data to save the NSManaged Objects created by CloudKit without the app going to the background?
Here is my code for setting up the container, and I do have the entitlements for remote notifications and push notifications set up. Also (application.registerForRemoteNotifications()) is set up in the App delegate. Am I missing something here?
Currently the app is dealing with a mix of NSManaged Objects and CKRecords to display in the table View that shows the items on a list. Which is a bit messy. I really want to just work with NSManaged Objects with Cloudkit doing the work of keeping things in sync between the 2 users.
When User B adds an Item it successfully saves to the Shared Database and sets the parent as the list that was shared. This is where I get a bit stuck as I want User A to see the Item that User B added and I want to use the NSManaged object that CloudKit makes in the background to update the UI.
In the background Cloudkit sees the new CKRecord that User B has added onto the shared database and makes a new NSManaged Object for User A. I can only get Core Data to save the new NSManaged Object Cloudkit makes when the app goes to the background or is closed then restarted. I do wait around a minute to make sure the sync happens. Checking the Sqlite database for user A the new Item added by User B isn't in there. When the app goes to the background and I check the Sqlite database again the Item is then added. It looks like the NSManaged object that Cloudkit makes is only saved to Core Data when the app goes to the background.
Long story short, Is there a way to get Core Data to save the NSManaged Objects created by CloudKit without the app going to the background?
Here is my code for setting up the container, and I do have the entitlements for remote notifications and push notifications set up. Also (application.registerForRemoteNotifications()) is set up in the App delegate. Am I missing something here?
Code Block lazy var persistentContainer: NSPersistentCloudKitContainer = { let container = NSPersistentCloudKitContainer(name: "Shopping_List") let description = container.persistentStoreDescriptions.first description?.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) // listen for remote notifications let remoteChangeKey = "NSPersistentStoreRemoteChangeNotificationOptionKey" description?.setOption(true as NSNumber, forKey: remoteChangeKey) container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo)") } }) container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy container.viewContext.automaticallyMergesChangesFromParent = true return container }()