Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get default project with NSPersistentCloudKitContainer up and running?

I followed the same steps as in "Using Core Data With CloudKit":

  1. New project
  2. Enable Core Data + Cloud Kit
  3. Add iCloud/CloudKit entitlement + Background mode/remote notifications entitlement.
  4. In the iPhone Xr simulator I signed into an iCloud account I created (and then verified on icloud.com!) and ran the app, creating multiple entries.
  5. I then signed into the same iCloud account in the iPhone Xs simulator. I ran the app but no entries were merged. Creating entries in this simulator also does not merge back over to the Xr simulator.

What am I missing?

like image 769
Epaga Avatar asked Jun 14 '19 16:06

Epaga


People also ask

What is nspersistentcloudkitcontainer?

NSPersistentCloudKitContainer seems too automatic/opaque. (I say this before the session has taken place, though.) It’s a whole new managed approach where Core Data owns the CloudKit container and handles it.

Why doesn’t CloudKit core data sync merge in remote changes?

So CloudKit Core Data Sync doesn’t merge in remote changes before syncing up the modified local record, and sends whole record not just changed fields, causing it to overwrite whatever changes another device made that was not yet received, seems like a dealbreaker. Update (2019-06-04): Malcolm Hall:

Why isn’t core data CloudKit using ckreference?

sadly Core Data CloudKit isn’t using CKReference for related records, just using a string field, thus losing integrity. I was really hoping they would make the public the CKReferenceActionValidate that Notes uses for e.g. the one-to-many folder notes relation. Update (2019-06-06): Tom Harrington:


1 Answers

To see the changes with Simulators you have to quit the app, and reload it (or build & run).

Simulators have never been able to receive Remote Notifications to trigger an iCloud sync so you need to manually force a sync, but I've found that syncing cannot be triggered manually from the menu in my Xcode 11 beta (gives an error).

There is a good post by Andrew Bancroft about some other things such as setting the automaticallyMergesChangesFromParent property to true, but this doesn't make a difference when using Simulators (EDIT: It does, but I didn't realise as I was just building & running each time).

Andrew's Post: https://www.andrewcbancroft.com/blog/ios-development/data-persistence/getting-started-with-nspersistentcloudkitcontainer/#where-s-my-data

I'm in the same boat as I can't afford to install any beta software (except Xcode) so I'm going to have to stick with the simulators. But my experience of converting an existing App to CloudKit has been very, very positive. I just had to do three things to my existing project:

  • Add Background Notification & CloudKit capabilities
  • Make sure all Core Data attributes & relationships are optional (or have a default value if nil)
  • Rename NSPersistentContainer to NSPersistentCloudKitContainer

...and that's it! Mind blown.

All my nested many-to-many relationships appear to work perfectly.

I still need to work out how to sync images currently stored as JPGs in the users Documents directory but I suspect they'll need to be stored in Core Data as BLOBs to enable conversion to CKAssets in the background.

like image 112
Paul Martin Avatar answered Oct 17 '22 22:10

Paul Martin