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.
#if DEBUG
do {
// Use the container to initialize the development schema.
try container.initializeCloudKitSchema(options: [])
} catch {
// Handle any errors.
}
#endif
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_[attribute.name]_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
https://developer.apple.com/documentation/coredata/sharing_core_data_objects_between_icloud_users
For an iOS device, choose Settings > Apple ID > iCloud, and turn on iCloud Drive, if necessary
Debugging
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
-com.apple.CoreData.ConcurrencyDebug 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] "iCloud.com.example.CloudKitCoreDataDemo"'
# Push
log stream --predicate 'process = "apsd" AND message contains[cd] "CoreDataCloudKitDemo"'
# Scheduling
log stream --predicate 'process = "dasd" AND
message contains[cd] "com.apple.coredata.cloudkit.activity" AND
message contains[cd] "CEF8F02F-81DC-48E6-B293-6FCD357EF80F"'
Read more
- https://developer.apple.com/documentation/coredata/mirroring_a_core_data_store_with_cloudkit
- General Findings About NSPersistentCloudKitContainer
- https://github.com/ggruen/CloudKitSyncMonitor
- https://beckyhansmeyer.com/2021/03/30/how-to-set-up-core-data-cloudkit-and-swiftui-when-you-havent-the-faintest-clue-what-youre-doing/
- https://roelvanderkraan.nl/nspersistentcloudkitcontainer-struggles/
- https://github.com/apple/cloudkit-sample-coredatasync