Resources – iCloud Sync

I recently had to add iCloud sync to one of my projects. Below are the list of resources that helped me. Hope they help you too.

Resources explaining what CloudKit is:
  • ☁️ Creating a Core Data Model for CloudKit: https://developer.apple.com/documentation/coredata/mirroring_a_core_data_store_with_cloudkit/creating_a_core_data_model_for_cloudkit – VERY GOOD ONE
    • Talks about how to create a CloudKit compatible model and initializing the schema
    • What features of Core Data, CloudKit is incompatible with?
    • Set shouldInitializeSchema to true.
      • Run your app on a device. Look for this log line in your console output: <NSCloudKitMirroringDelegate: 0x7f9699d29a90>: Successfully set up CloudKit integration for store
    • Set shouldInitializeSchema to false.
      • Initialize your schema one last time, then promote it from the CloudKit dashboard.
    • During initialization, Core Data creates representative CKRecord instances for all stores in the container that use Core Data with CloudKit and uploads them to CloudKit. Finally, the schema becomes visible in the CloudKit dashboard, and Core Data deletes the representative records.
      • Reset shouldInitializeSchema after initialization to ensure that you only create these records once.
    • Consider what happens if a user deletes a record from their phone : -> try? persistentContainer.viewContext.setQueryGenerationFrom(.current)
    • Shows techniques to debug
    • Inspect Logs to See What Happened
  • ☁️ NSPersistentCloudKitContainer in Production: https://www.grantgrueninger.com/2020/01/nspersistentcloudkitcontainer-in-production
    • Talks about how CloudKit is operation-based
    • Your production schema must be correct – In your CloudKit dashboard (with which you should become extremely familiar), you must initialize your development schema and push it to production before you push your app anywhere (even to TestFlight internal testers).
    • You must make sure all devices have new schema changes before enabling features that use them
    • MacCatalyst Save (and sync) when resigning active & quitting
    • One way to handle DB schema changes in upgrades is to use feature toggles: launch new features that require DB updates with the user-facing functionality turned off. Then, a version or two later, turn the features on.
  • ☁️ Talks about initializeCloudKitSchema: https://developer.apple.com/forums/thread/120453
    • NSPersistentCloudKitContainer materializes a number of specific values in your record that may not be initialized properly simply by using your app. Specifically for variable length fields like data blobs and strings you are unlikely to trivially violate our asset threshold during normal use.
  • ☁️ Public CloudKit with CoreData: <Has code example>https://patmalt.medium.com/end-to-end-encryption-and-cloudkit-with-coredata-part-1-67bfbe467bc GOOD ONE
    • Talks about the steps to
    • Upload Schema : try container.initializeCloudKitSchema(options: NSPersistentCloudKitContainerSchemaInitializationOptions())
    • & Update Schema : add 2 indices for this to Core Data to work correctly. Each entity needs modifiedAt and recordName as queryable indices
    • Once you have finalized your schema, add the following code to initialize the schema in iCloud. You do not need to call this method unless you have changed your schema since the last time the schema was initialized.
🌨 Check if data is synced:
  • 🌧 Toggle sync with NSPersistentCloudKitContainer: https://developer.apple.com/forums/thread/118924
    • if you want to use the asynchronous method but don’t care about why, you should check that accountStatus is available, rather than checking that it is not noAccount

⛈ CloudKit dashboard:

🌦 CloudKit Schema deployment:
🌤 Code examples:
🌪 Issues / Fixes:


Interested in the code of my iOS app – The Bhagavad Gita Interpretation ?

I have created a project – “My Book App” containing the same codebase & techniques used in the published app.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: