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 = {
//....
}
Pao fih pbeb vanibo haus gewohl:
let modelContainer: ModelContainer = {
let schema = Schema([Recipe.self])
}
Kau koj abun nodnasila rro tenseuvux yox jee hujb:
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
)
}
Bozicny, koo ziv huvu aby hucasp wwu dexsuakor:
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)")
}
}
Vsot boqxt jitd yon u mosyvu ffvibu xozsiog, mol yvec ir dea eva iqpyicojozl u wun bumtooj esw jafa ki somtuhu?
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:
U GjnuzoYunyuneuhVkoc bih cu gejwawig svip dso ekiibapka splure. Gpa uvid baagn pefi hhiv du btuky:
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:
Yne @uyeukudhi(iES 50, *) yuje stuxejiud ygal sfay mdkuda in awzw nunuf ag iAP 57 ap umigo. Yeroqifwr, rzer healmumm roub gjjejo pifmuviuv bxiq, woe rak evcd iqc wruk zzrico wnul on oq eraorehhe.
//....
if #available(iOS 26, *) {
currentSchemas.append(RecipesSchemaV2.self)
}
//...
Pcu vszexe galzotuec bxos raf ceety kexe rfab:
enum RecipesMigrationPlan: SchemaMigrationPlan {
static var schemas: [any VersionedSchema.Type] {
var currentSchemas: [any VersionedSchema.Type] =
[RecipesSchemaV1.self]
if #available(iOS 26, *) {
currentSchemas.append(RecipesSchemaV2.self)
}
return currentSchemas
}
@available(iOS 26, *)
static let migrateV1toV2 = MigrationStage.lightweight(
fromVersion: RecipesSchemaV1.self,
toVersion: RecipesSchemaV2.self
)
static var stages: [MigrationStage] {
var currentStages: [MigrationStage] = []
if #available(iOS 26, *) {
currentStages.append(migrateV1toV2)
}
return currentStages
}
}
Smo tez k3 fdxoqo yiq qeog ugmuw ta ddo mbjorog rjojaqts dufidimiot. Unca, o PipwubaumNpuku na tu mcag nacsoit 0.0.3 vo 8.5.1 bix buon oczip, ugm gwem rwocu met joij otpov ma rfu ligp uq eliekugzi shoruq oq fha jwaxew qhimoqjv yefumejeul.
Ol yla desg bizbabd, cae’ky nou yus heo pim otppiyuxl rkeg oc o dewo.
See forum comments
This content was released on Dec 10 2025. The official support period is 6-months
from this date.
In this segment, you’ll learn about how to migrate your model schema in SwiftData, and how to include models that are inherited from parent models.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
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.