Lightweight Migrations and CloudKit Sync
Migrating schema changes and enabling CloudKit sync with NSPersistentCloudKitContainer.
Why Migrations?
When you change your Core Data or SwiftData model (add/rename/remove attributes), you must migrate existing stores.
// Without migration, app crashes on launch:
// "The model used to open the store is incompatible with the one used to create the store"Lightweight Migration in Core Data
Core Data can automatically infer simple migrations (add attribute, rename, remove) without a mapping model.
let desc = NSPersistentStoreDescription(url: storeURL)
desc.shouldMigrateStoreAutomatically = true
desc.shouldInferMappingModelAutomatically = true
container.persistentStoreDescriptions = [desc]All lessons in this course
- Core Data Stack: NSPersistentContainer Setup
- SwiftData @Model and ModelContext
- Relationships and Fetch Descriptors
- Lightweight Migrations and CloudKit Sync