I’m experiencing a super weird/annoying quirk where modifying an @ObservedRealmObject property, the view dismisses or pops to the previous view in the navigation stack.
The oddest part is that modifying properties on other objects also using the same property wrapper aren’t popping / dismissing.
One of them is a linked object on the other, so maybe that’s the issue?
Here are how the objects are modeled:
class Plan: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id = UUID()
@Persisted var location: Location?
@Persisted var name: String = ""
}
class Location: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id = UUID()
@Persisted var name: String = ""
}
If I write to the Plan using $plan.name it works.
I can’t write to the Location using $plan.location.name but I can pass it into its own view and write to $location.name, each key stroke dismisses the view.
My workaround is to move the props I need to write to into the Plan object instead of the Location object, but if anyone has seen this behavior or understands why it’s happening, I’d really like to know how to handle the data correctly so that this won’t happen.
So, you’re passing $location.name directly to textField? That’s not something I’ve tried as I always pass a local state variable and then use that to update the realm objects when done. I can give it a try.
Yeah, that’s for the implicit writes. I’m trying absolutely everything ( trying to switch between @ObservedRealmObject, @StateRealmObject, @Binding, no property wrappers, etc. ) to figure out what’s causing it, but I can’t figure it out.
Basically everything I try in this app is plagued by this where I just can’t keep working on it. So frustrating.
I got to the point where I am trying to provide edit screens and every time you type to edit a field, every keystroke pops the view and the last thing I want to do is start mapping every stored property to a local property that then watches for changes and then writes on blur or dismissing screen or a save button to then update the object with changes from state variables. That is exactly what these implicit writes are supposed to be preventing.
Hi @Kurt_Libby1, not sure if I’m matching exactly what you’re trying to do here, but I did reproduce some odd behavior when updating location.name in a TextField: repo
If I type slowly when editing the location name, it works fine. If I type quickly then it locks up - and if I look at the realm file in Realm Studio I can see that the value rapidly toggles between the before and after values.
I’ve been playing around with your simplified repo and I was finally able to reproduce it.
I have a list of plans that i can then go to the detail screen. So there is a NavigationView with a NavigationLink(destination: PlanDetails(plan: plan).
Then, on the PlanDetails page I have an edit button in the toolbar that is also a NavigationLink(destination: EditLocation(location: location).
All of the editing works until the 2nd level of navigation. Once the second level of NavigationLink is introduced, every keystroke pops up a level.
I still have no idea why it’s doing this, but this should be easily reproducible now. Forked and added the 3rd level of Nav Stack here: GitHub - misterlib/PlanTest at addNavStack
According to this post, it’s a SwiftUI issue with all ObservedObjects, not just the Realm flavor.
Adding .isDetailLink(false) to the NavigationLink stops it. Something about the columns structure for iPad and the way that the links are handled, this allows the screen in the stack to stay put.