-
Type: Bug
-
Resolution: Fixed
-
Priority: Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: None
Goals
I'm trying to delete a realm object that is used across multiple NavigationViews. I'm using the @ObservedResults and @ObservedRealmObject Wrapper demonstrated in this session here from @jsflax : https://www.youtube.com/watch?v=mTv96vqTDhc in the latest Release 10.6.0
Expected Results
I'm expecting the Realm Object to be deleted and the app to remain healthy / not to crash. I'm not sure if I follow the intention of those wrappers since I wasn't able to find the "advertised" best practices / docu from the session. If the error is caused by me please quickly outline where I'm wrong.
Actual Results
Once I deleted the Realm Object and navigate back to the root view the app crashes with the error:
"Cannot remove an observer <RealmSwift.SwiftUIKVO 0x6000000c00e0> for the key path \"myId\" from <RLM:Unmanaged MyModel 0x6000037fa8a0> because it is not registered as an observer."
Steps for others to Reproduce
I assembled a super easy Demo Project to reproduce the error. Just run this and go to the third View where you wanna click on the button before you try to navigate back.
Code Sample
import SwiftUI import RealmSwift //minimum version 10.6.0 Realm and 10.5.0 RealmDatabase from https://github.com/realm/realm-cocoa using Swift Package Manager //MARK: - Realm Model @objcMembers class MyModel: Object, ObjectKeyIdentifiable { dynamic var myId = "" dynamic var text = "" dynamic var desc = "" override static func primaryKey() -> String? { return "myId" } } //MARK: - Root View struct ContentView: View { @ObservedResults(MyModel.self) var myModel @State var navigationViewIsActive: Bool = false var body: some View { NavigationView { VStack { Text("Click the plus in the toolbar to add some test entries") List { ForEach(myModel) { model in NavigationLink(destination: SecondView(navigationViewIsActive: $navigationViewIsActive, selectedEntry: model)) { Text(model.text) } } } } .toolbar { ToolbarItem(placement: .navigationBarLeading){ Button(action: { let model = MyModel() model.myId = UUID().uuidString.lowercased() model.text = UUID().uuidString.lowercased() do { let realm = try Realm() try realm.write { realm.add(model, update: .modified) print("adding new entry to models") } } catch let error { print(error.localizedDescription) } }, label: { Image(systemName: "plus") }) } } } } } //MARK: - SecondView struct SecondView: View { @Binding var navigationViewIsActive: Bool @ObservedRealmObject var selectedEntry: MyModel @State var showThirdView = false var body: some View { VStack { Text("Selected Entry:") Text(selectedEntry.text) Button(action: { print("show third view") self.showThirdView = true }, label: { Text("Click to show Third View") }) } NavigationLink(destination: ThirdView(navigationViewIsActive: $navigationViewIsActive, selectedEntry: selectedEntry), isActive: $showThirdView, label: {EmptyView()}) } } //MARK: - Third View struct ThirdView: View { @Binding var navigationViewIsActive: Bool @ObservedRealmObject var selectedEntry: MyModel var body: some View { VStack { Text("Selected Entry:") Text(selectedEntry.text) Button(action: { print("delete this entry") self.navigationViewIsActive = false do { let realm = try Realm() try realm.write { let myEntryIwantToDelete = realm.objects(MyModel.self).filter("myId=%@", selectedEntry.myId) realm.delete(myEntryIwantToDelete) } print("deleting entry via realm") } catch { print("Error") } }, label: { Text("Delete this entry and go back to root view") }) Text("after i click this button and navigate back i get the error") } } }
Version of Realm and Tooling
ProductName: macOS ProductVersion: 11.1 BuildVersion: 20C69 /Applications/Xcode.app/Contents/Developer Xcode 12.4 Build version 12D4e iPhone 11 Simulator iOS 14.4 pod not found (not in use here) /bin/bash GNU bash, version 3.2.57(1)-release (arm64-apple-darwin20) carthage not found (not in use here) /usr/bin/git git version 2.24.3 (Apple Git-128)