ios – crashed while using @EnvironmentObject with realm in swiftui


I really need your help cause it seems beyond my ability.
i’ve stuck in this black hole almost all yesterday😓😓

I constructed a minimization program to simplify things:

Model

import RealmSwift

class Space: Object, Identifiable {
    @Persisted(primaryKey: true) var id: ObjectId
    @Persisted var name: String = ""
    
    convenience init(name: String) {
        self.init()
        self.name = name
    }
}

ViewModel

import Foundation
import RealmSwift

class SpaceStore: ObservableObject {
    @Published var spaces: Results<Space>?
    static let store = SpaceStore()
    
    private static let realm = try! Realm()
    private var realmToken: NotificationToken?
    
    init() {
        let results = SpaceStore.realm.objects(Space.self)
        realmToken = results.observe { _ in
            self.spaces = results
        }
    }
    
    func add(by name: String) {
        let space = Space(name: name)
        try! SpaceStore.realm.write {
            SpaceStore.realm.add(space)
        }
    }
    
    func delete(_ space: Space) {
        try! SpaceStore.realm.write {
            SpaceStore.realm.delete(space)
        }
    }
}

ContentView

import SwiftUI

struct ContentView: View {
    @EnvironmentObject var store: SpaceStore
    
    var body: some View {
        Button("Add") {
            store.add(by: "qwerty")
        }
        Divider()
        if let spaces = store.spaces {
            ForEach(spaces) { space in
                HStack {
                    Text(space.name)
                    Button("Delete") {
                        store.delete(space)
                    }
                }
            }
            Divider()
            if let spaceItem = spaces.first {
                ItemView(space: spaceItem)
            }
            Divider()
            ForEach(spaces) { space in
                // RowView(space: space)
            }
        }
        
    }
}

#Preview {
    ContentView()
        .environmentObject(SpaceStore.store)
}

ItemView

import SwiftUI

struct ItemView: View {
    let space: Space
    @EnvironmentObject var store: SpaceStore
    
    var body: some View {
        HStack {
            Text(space.name)
            Button("Delete") {
                store.delete(space)
            }
        }
    }
}

#Preview {
    ItemView(space: Space(name: "e"))
        .environmentObject(SpaceStore.store)
}

and the RowVIew is almost the same as ItemView except name so I omitted it.


here is the weird things during debugging :

  1. delete inside the ContentView, it’s ok
  2. delete in the ItemView for just one item, it’s ok
  3. delete in the RowView(inside foreach), boooooom~
    ps: while i import the RowView, delete action by 1&2 turn to be crashed.

then, I replaced the @EnvironmentObject var store: SpaceStore with let store: SpaceStore in RowView, everything seems to be working well.

at last, I tried to wrap the delete action in a confirmationDialog like this:

.confirmationDialog("", isPresented: $ifShowSheetOfSettings) {
   Button("delete") {
      store.delete(space)
   }
}

it crashed again…

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img