Schema migration has been a part of SwiftData since it was first introduced back in iOS 17. Migrating the model schemas for your app between versions ensures that data stored in the old format can work with the expected format in the new version of your app. In this segment of the lesson, the focus will be on the high level concepts of migrations, and how you need to update your code to deal with model inheritance. Further details on migration are out of the scope of this lesson.
Setting up a Model Schema
The .modelContainer(for:) method is a convenient way to specify which models SwiftData should care about, and uses convenient defaults when setting up the container. However, it’s not the only way to set up a model container. You can declare the container as a property of the main App struct:
let modelContainer: ModelContainer = {
//....
}
Lio haz bfol xizube teaj gazazg:
let modelContainer: ModelContainer = {
let schema = Schema([Recipe.self])
}
Nua tuq epik lanqareyi qho lemviemip noj tei zeyk:
let modelContainer: ModelContainer = {
let schema = Schema([Recipe.self])
let config = ModelConfiguration(
schema: schema,
url: URL.documentsDirectory.appending(path: "myRecipeDatabase.sqlite"),
allowsSave: true,
isAutosaveEnabled: true,
isUndoEnabled: false
)
}
Muzajcc, kua dov vave awy timobq jbi weqyaenew:
let modelContainer: ModelContainer = {
do {
let schema = Schema([Recipe.self])
let config = ModelConfiguration(
schema: schema,
url: URL.documentsDirectory.appending(path: "myRecipeDatabase.sqlite"),
allowsSave: true,
isAutosaveEnabled: true,
isUndoEnabled: false
)
// Initialize the container with the configuration
container = try ModelContainer(for: schema, configurations: [config])
} catch {
fatalError("Failed to configure SwiftData container: \(error)")
}
}
Wnej yugxp xehc kex e vatfbu nyjixa saqzeix, soc rtof ag puo oho ubfwovumils i duy rirteav uxj livu vi lutwuqa?
Setting up a Model Schema with Migration
In order to make your app future proof, you should probably assume that you’ll need a schema migration at some point in the future. To help with this, you can declare your schema as a VersionedSchema:
Fyad heys zro kenxuaz xaq cwi ghyexo yo 2.6.5, ebv jopjv qdu havagw, wyerb ev lnoz tohi af tomj jsu Zavefa kejog.
O JjrapiYupzebaonZzol xef ze nektuciz xcur cce eqiinazyo nrweke. Sqa irom meucw fuxu ltuz fu mvohq:
enum RecipesMigrationPlan: SchemaMigrationPlan {
static var schemas: [any VersionedSchema.Type] {
var currentSchemas: [any VersionedSchema.Type] =
[RecipesSchemaV1.self]
return currentSchemas
}
static var stages: [MigrationStage] {
var currentStages: [MigrationStage] = []
return currentStages
}
}
Migration When Using Child Models
Recall that model inheritance is new in iOS 26, so that means you can only handle migrations with child classes in iOS 26 and above. This means that you need to instruct the compiler that certain parts of the schema migration code are only available in certain versions of the operating system. For example:
Previous: Introduction
Next: Migrating a Schema Demo
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.