Post

Replies

Boosts

Views

Activity

SwiftData EXC_BAD_ACCESS(code=2... on construction of ModelContainer
Hi, I'm making this post partly as a bug report, and partly to help others that run into what I just did using SwiftData (and probably happens in CoreData). I have a model I just added in my app that looks kinda like this: @Model final class Reference { // represents a "reference" to an external web source var project: Project } and I tried adding a relationship to a parent model: @Model final class Project { @Relationship(deleteRule: .cascade, inverse: \Reference.project) var references = [Reference]() } I then started getting a crash with EXC_BAD_ACCESS(code=2 on Previews, and running it in simulator/device revealed the crash to be happening on the ModelContainer initialization at the App startup: After hours of searching for a solution, I saw some mention of similar unexplained crashes occurring due to some property names that CoreData doesn't like, but are not documented as such. So I changed the "references" variable in Project to "projectReferences" on a hunch ("references" is a core programming concept, so not far fetched there's a conflict somewhere), and that didn't work, neither did cleaning the build. However, by reverting to a previous commit that didn't include the Reference model, running the build in Simulator (which finally started up the app clean), and then re-adding my changes adding Reference with the new relationship property name, the app finally started up! So if you run into a crash like this on ModelContainer() init, try changing recently added property names in your models!
0
0
670
Jan ’24