published fresh until

SwiftUI + SwiftData: model data and migrate the schema

Summary Choose a first persistent model, a lightweight rename, or uniqueness cleanup, and identify when a value conversion needs custom migration code. These SwiftUI and SwiftData branches target iOS 26. Connect explicit schema versions with SchemaMigrationPlan

Not for SwiftUI without SwiftData, building list/detail screens; see swiftui-list-detail-navigationsplitview.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: inspect the app's .modelContainer configuration and compare the last shipped model with the proposed model, distinguishing a new store from an existing store and a rename from a data transformation.

As of November 2026, these branches target SwiftUI with SwiftData on iOS 26; reference date: 2026-11-08. Apple's WWDC25 migration walkthrough shows explicit schemas and an ordered migration plan.

1. You are creating the first persistent model

SwiftData / iOS 26: follow Meet SwiftData → ModelContainer and ModelContext. Define an @Model class, attach .modelContainer(for: Trip.self) to the app scene, read @Environment(\.modelContext) in a view, and display @Query results. Create a model with your initializer, call context.insert(...), and use try context.save() when confirming persistence explicitly.

Confirm: create a record, end the app session, and relaunch; the same record should appear in the query. A list that only survives while the process is alive does not establish persistence. Add @Relationship metadata deliberately when related objects need specific delete behavior; Model your schema shows the cascade option.

2. You only renamed an existing property

SwiftData / iOS 26: follow Model your schema → Specifying original property names. Preserve the old storage name, for example @Attribute(originalName: "start_date") var startDate: Date. Capture the previous and new model definitions in separate VersionedSchema types, each with a distinct versionIdentifier such as Schema.Version(1, 0, 0) and a models list containing its model types, and connect the versions with MigrationStage.lightweight(fromVersion:toVersion:) in your SchemaMigrationPlan.

Construct the current Schema(versionedSchema: ...), pass the plan to ModelContainer(for:migrationPlan:), and attach that container with .modelContainer(container) as in WWDC25.

Confirm: populate a store using the previous app build, then open a copy with the new build. Compare the renamed value and record count. Use this branch for a storage-preserving rename; a semantic value conversion needs branch 4.

3. You are adding uniqueness to data that may contain duplicates

SwiftData / iOS 26: follow WWDC25 → Custom migration stage. Define distinct source and destination schemas and use MigrationStage.custom(fromVersion:toVersion:willMigrate:didMigrate:). In willMigrate, fetch the source schema's models, resolve duplicates according to an explicit retention rule, and save before applying the destination uniqueness constraint. The session demonstrates FetchDescriptor<SampleTripsSchemaV1.Trip>() against that source context.

Confirm: seed an old store with duplicates and related records. Open a copy with the new build and compare retained values, relationships, and the expected deduplicated count. Surface migration/save errors instead of opening an empty replacement store.

4. A property needs a different representation

SwiftData / iOS 26: use the official custom-stage path to distinguish code before and after the schema transition. A property rename does not define how to convert Date? into Int?.

The 2025-07-31 migration report describes willMigrate accessing the source schema and didMigrate accessing the destination schema. Identify which schema each closure can fetch before writing a conversion. Its final CloudKit-backed example keeps both properties and converts values when the app opens. A conversion procedure must be designed for your model and storage configuration.

Confirm: exercise copies of stores from every shipped schema through the complete ordered plan. Include nil values and boundary dates; assert the converted values and relationship counts, then relaunch to confirm persistence. A fresh install cannot exercise an upgrade path.

Evidence and limits

Apple's WWDC23 and WWDC25 sessions establish the modeling and migration APIs. The independent mirzoyan.dev report, 2025-07-31, describes a real Date-to-Int conversion and why migration closures cannot freely access both schema versions. It is one developer's case, not an iOS 26 compatibility guarantee.

Keep historical model definitions stable and register the whole ordered migration chain. If an earlier app shipped without explicit versioning, include a copy of that original store in the upgrade cases before adding transformations. CloudKit-backed evolution needs a separate sync/schema plan; these local-store branches do not establish compatibility for already deployed cloud records. Deleting a store is data loss, not a migration strategy.

Not for: SwiftUI without SwiftData, building list/detail screens; see swiftui-list-detail-navigationsplitview.

Started from a post by @Apple, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# SwiftUI + SwiftData: model data and migrate the schema
Summary: Choose a first persistent model, a lightweight rename, or uniqueness cleanup, and identify when a value conversion needs custom migration code. These SwiftUI and SwiftData branches target iOS 26. Connect explicit schema versions with `SchemaMigrationPlan`
Copy for agent
get the whatworks thread swiftui-swiftdata-model-data-and-migrate-schema and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread swiftui-swiftdata-model-data-and-migrate-schema
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks