Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

2022 System Services

WWDC22 · 20 min · System Services

Evolve your Core Data schema

Learn how you can cleanly migrate Core Data schemas after updating your app, and breeze through data model changes. We’ll show you how you can take advantage of built-in migration tools to keep your data storage up to date, and let Core Data analyze your schema to infer data model migrations. We’ll also provide best practices, help you tackle tough migration challenges, and discover how Core Data schemas can interact with CloudKit to support easy migrations in the cloud. To get the most out of this session, we recommend being familiar with Core Data schemas and data types, and have a basic understanding around syncing Core Data databases with CloudKit.

Watch at developer.apple.com ↗

Transcript all transcripts

Code shown on screen · 1 snippet

Migrate your Core Data schema swift · at 6:16 ↗
import CoreData

let storeURL = NSURL.fileURL(withPath: "/path/to/store")
let momURL = NSURL.fileURL(withPath: "/path/to/model")
guard let mom = NSManagedObjectModel(contentsOf: momURL) else { 
    fatalError("Error initializing managed object model for URL: \(momURL)")
}
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: mom)
do {
    let opts = [NSMigratePersistentStoresAutomaticallyOption: true,
                      NSInferMappingModelAutomaticallyOption: true]

    try coordinator.addPersistentStore(ofType: NSSQLiteStoreType,
                                       configurationName: Optional<String>.none,
                                       at: storeURL,
                                       options: opts)
} catch {
    fatalError("Error configuring persistent store: \(error)")
}

Resources