Post

Replies

Boosts

Views

Activity

Reply to SwiftData crash on fetch
I have some potential progress, but due to the nature of this crash, I don't know if this is correct or not. After inspecting the sqlite-files that represents the backingdata, I discovered that a property backed by an integer was set to null after migration even the original value was 3. Upon inspection, I realized this was the very first property I ever migrated, so it was still annotated with @Attribute(orginalName: "propertyName"), even tho the migration which crashed for me was the 9th migration step overall for the app. Thus, with the Schema I was migrating from being: struct SchemaV7 { @Model class Item { @Attribute(originalName: "propertyName") var newPropertyName: Int } } And then removing it in the destination Schema: struct SchemaV8 { @Model class Item { var newPropertyName: Int } } made me able to complete the migration without crashing. Wether this was the solution or not, I have no idea, but it could explain some of the crashes I've seen earlier... The property which was crashing the app have never been optional, but the sqlite column driving the backingstore was set to optional, which explains the crash. It doesn't however explain the data loss on migration
Jun ’25
Reply to SwiftData crash on fetch
The logs after a failed migration could maybe be relevant: CoreData: annotation: Completed persistent history metadata tables update CoreData: annotation: Updating metadata CoreData: annotation: Finished updating metadata CoreData: annotation: Committing formal transaction CoreData: annotation: Finished committing formal transaction CoreData: annotation: Checkpointing WAL journal CoreData: annotation: Finished checkpointing WAL journal CoreData: annotation: Successfully completed lightweight migration on connection CoreData: annotation: Migration step 0.0 'Total migration time (on connection)' took 0.02 seconds CoreData: annotation: (migration) in-place migration completed successfully in 0.02 seconds Did migrate to stage 8 CoreData: annotation: (migration) Automatic schema migration succeeded for store at 'file:///var/mobile/Containers/Data/Application/74E75823-0F85-4194-A84D-B180739009BB/Library/Application%20Support/default.store' with migration stage: <NSCustomMigrationStage: 0x107220990> SwiftData/PersistentModel.swift:734: Fatal error: What kind of backing data is this? SwiftData._KKMDBackingData<Impact.ImpactSchemaV8.TrajectoryMode> This time, the app is crashing on a different property than the previous crash...
May ’25
Reply to SwiftData crash on fetch
The images below are displaying the only difference between the two schemas: This is the migration plan: enum ImpactMigrationPlan: SchemaMigrationPlan { static var schemas: [any VersionedSchema.Type] { [ ImpactSchemaV1.self, ImpactSchemaV1_1.self, ImpactSchemaV2.self, ImpactSchemaV3.self, ImpactSchemaV4.self, ImpactSchemaV5.self, ImpactSchemaV6.self, ImpactSchemaV7.self, ImpactSchemaV8.self ] } static var stages: [MigrationStage] { [migrateV1toV1_1, migrateV1_1toV2, migrateV2toV3, migrateV3toV4, migrateV4toV5, migrateV5toV6, migrateV6toV7, migrateV7toV8] } .... static let migrateV7toV8 = MigrationStage.custom( fromVersion: ImpactSchemaV7.self, toVersion: ImpactSchemaV8.self, willMigrate: { context in Logger.application.info("Will migrate to stage 8") }, didMigrate: { context in Logger.application.info("Did migrate to stage 8") }) And this is how the model container is built in the app: var modelContainer: ModelContainer = { let modelContainer: ModelContainer let schema = Schema(versionedSchema: ImpactSchemaV8.self) let config = ModelConfiguration(cloudKitDatabase: .none) do { modelContainer = try ModelContainer( for: schema, migrationPlan: ImpactMigrationPlan.self, configurations: config ) } catch { Logger().error("Error creating model container: \(error.localizedDescription)") preconditionFailure("Failed to create model container") } return modelContainer }()
May ’25
Reply to SwiftData crash on fetch
Apple DTS asked me for a link to the forums, so I'm adding what I know, and send them here. The crash is hard to reproduce, so this is what I know as of now: My app is a SwiftUI-app, with SwiftData I create the ModelContainer in the App-structure: var modelContainer: ModelContainer = { let modelContainer: ModelContainer let schema = Schema(versionedSchema: SchemaV7.self) let config = ModelConfiguration(cloudKitDatabase: .none) do { modelContainer = try ModelContainer( for: schema, migrationPlan: MigrationPlan.self, configurations: config ) } catch { Logger().error("Error creating model container: \(error.localizedDescription)") preconditionFailure("Failed to create model container") } return modelContainer }() When I add a new Schema, the migration completes successfully, but for some users (not all), the app crashes. If I delete the app and install the same app but with no database, the app runs as normal. Migrations are done as custom migration steps, to provide logging options, but the crash happens still if the step is lightweight migration
May ’25
Reply to SwiftData assertionFailure crash in release builds?
Thank you for your answer! I realized I had made a post on the exact same issue in October 2024, which can be found here: https://developer.apple.com/forums/thread/765423 The main problem is that this seems to be an issue with iOS 18, that I haven't found a way to reproduce consistently. Since I don't have a code example, I can't open a TSI. The thing that makes me believe this is an issue in iOS, is the fact that the first frame in the stack trace of the crash report is an assertionFailure which I believe shouldn't happen in a release build with optimization set to -O
Apr ’25