Issue #879

Setting Up Core Data with CloudKit

  • Enable iCloud
  • Enable CloudKit and Push Notifications
  • Enable Remote Notifications in the Background

Creating a Core Data Model for CloudKit

Initialize Your CloudKit Schema During Development

let container = NSPersistentCloudKitContainer(name: "Earthquakes")

// Only initialize the schema when building the app with the 
// Debug build configuration.        
do {
    // Use the container to initialize the development schema.
    try container.initializeCloudKitSchema(options: [])
} catch {
    // Handle any errors.

Reading CloudKit Records for Core Data

All variable length attribute types—String, Binary Data, and Transformable—generate an additional field with a key in the form CD_[]_ckAsset. If a field’s value grows too large to store within the record size limit of 1MB, Core Data automatically converts the value to an external asset. Core Data transitions between the original field and its asset counterpart transparently during serialization. When inspecting a CloudKit record directly, check the length of the original field’s value; if it is zero, look in the asset field.

For example, an entity named Post with String content and title attributes would generate the following fully materialized record, with pairs of fields for CD_content and CD_content_ckAsset, and for CD_title and CD_title_ckAsset.

iCloud Drive

For an iOS device, choose Settings > Apple ID > iCloud, and turn on iCloud Drive, if necessary


Debugging Core Data

Enabling this launch argument causes your App (or unit test) to throw an exception if you mistakenly access a managed object on the wrong queue 1

Optimize your use of Core Data and CloudKit

# Application
log stream --predicate 'process = "CoreDataCloudKitDemo" AND 
                        (sender = "CoreData" OR sender = "CloudKit")'

# CloudKit 
log stream --predicate 'process = "cloudd" AND
                        message contains[cd] ""'

# Push
log stream --predicate 'process = "apsd" AND message contains[cd] "CoreDataCloudKitDemo"'

# Scheduling
log stream --predicate 'process = "dasd" AND 
                        message contains[cd] "" AND
                        message contains[cd] "CEF8F02F-81DC-48E6-B293-6FCD357EF80F"'

Read more