I’m encountering an issue where my app crashes when users update to a new version.
I reproduced the error locally and basically it happens when I run the app with the new stored field added to the SwiftData model, but on the previous version of the database (which is exactly what happens upon releasing a new version of the app — users have new code but the database is from the previous version).
I expected SwiftData to handle this change automatically (via lightweight migration) by adding the new field to the database without requiring custom migration code.
Here’s an example of the added field (CloudKit is enabled for the app):
final class TodoItem {
var title = ""
var content = ""
var fontName: String = "Times" // <- new field
}
The new field has a default value, so I assumed the migration would be smooth and automatic. However, the app crashes on the first launch after the update but succeeds on subsequent launches.
How can I resolve this issue and ensure a smooth migration process?