realm-swift
realm-swift copied to clipboard
App crashes with BAD_ACCESS on saving object with one-to-many association
How frequently does the bug occur?
All the time
Description
As title states, app crashes with BAD_ACCESS on saving object with one-to-many association. Objects used in my tests are:
class CategoryEntity: Object, Identifiable {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var name: String
@Persisted var iconName: String
@Persisted var mainColor: String
@Persisted var secondaryColor: String
@Persisted var _partitionValue: String = AppScheme.partitionValue
@Persisted var subcategories: List<SubcategoryEntity>
}
class SubcategoryEntity: Object, Identifiable {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var name: String
@Persisted var iconName: String
@Persisted var mainColor: String
@Persisted var secondaryColor: String
@Persisted var _partitionValue: String = AppScheme.partitionValue
}
I just create a CategoryEntity
object, then I associate SubcategoryEntity
objects from a list (every row has a toggle that adds\removes SubcategoryEntity
from CategoryEntity
).
If I try to save (I store all variables in @State
variables in the view, then I put them in the object in a realm.write
transaction) it always crashes with BAD_ACCESS. This happens only with XCode 14, if I try same code with XCode 13 it works.
Here you can find XCode screenshot on crash
Stacktrace & log output
No response
Can you reproduce the bug?
Yes, always
Reproduction Steps
No response
Version
10.28.6
What SDK flavour are you using?
MongoDB Realm (i.e. Sync, auth, functions)
Are you using encryption?
No, not using encryption
Platform OS and version(s)
iOS 16
Build environment
Xcode version: 14.0 (beta 5)
Hi @Nerkyator have you checked out our SwiftUI property wrappers. You may find that @StateRealmObject
might solve your issue, see https://www.mongodb.com/docs/realm/sdk/swift/swiftui-tutorial/
Also the link for the screenshot doesn't seem to work for me, could you post it here?
Hi @Nerkyator have you checked out our SwiftUI property wrappers. You may find that
@StateRealmObject
might solve your issue, see https://www.mongodb.com/docs/realm/sdk/swift/swiftui-tutorial/Also the link for the screenshot doesn't seem to work for me, could you post it here?
HI @leemaguire , I just clicked on screenshot link and it works 🤨. This is the image url https://postimg.cc/VS1jPd32 Thanks for sharing me the SwiftUI property wrappers, I already use them but bug seems to be present anyway (maybe I use them wrongly? 🤔) I attach here my sample project. Deployment target is 15.5 so you can open it with XCode 13 and see that everything works but if you compile it with XCode 14 it crashes regardless of the iOS version.
Please note: I use the save button because I want the object to be updated on db only when user presses Save and not when he edits fields)
Hi @Nerkyator Thanks for the sample app! I can reproduce the crash when I go to add an item, tap 'Subcategories', go back and tap save. We'll investigate this now.
Just an initial observation, @ObservedRealmObject var category: CategoryEntity
in EditCategoryScreen
should use @StateRealmObject
as the category
object can have an unmanaged state in your app. @ObservedRealmObject
should not be used with unmanaged objects (that's on our side to fix by throwing an error).
Just an initial observation, @ObservedRealmObject var category: CategoryEntity in EditCategoryScreen should use @StateRealmObject as the category object can have an unmanaged state in your app. @ObservedRealmObject should not be used with unmanaged objects (that's on our side to fix by throwing an error).
It was one of my tries but, if I well understand, @StateRealmObject
needs an initial value that cannot be changed and this prevents to edit an object (ex: if click on a row of ListCategoriesScreen
it opens the EditCategoryScreen
sheet but always with a new object, not the selected one)