Home IOS Development ios – Crashed whereas utilizing @EnvironmentObject with Realm

ios – Crashed whereas utilizing @EnvironmentObject with Realm

0
ios – Crashed whereas utilizing @EnvironmentObject with Realm

[ad_1]

I constructed a minimization program to simplify issues:

Mannequin:

import RealmSwift

class House: Object, Identifiable {
    @Persevered(primaryKey: true) var id: ObjectId
    @Persevered var title: String = ""
    
    comfort init(title: String) {
        self.init()
        self.title = title
    }
}

ViewModel:

import Basis
import RealmSwift

class SpaceStore: ObservableObject {
    @Revealed var areas: Outcomes<House>?
    
    non-public static let realm = attempt! Realm()
    non-public var realmToken: NotificationToken?
    
    init() {
        let outcomes = SpaceStore.realm.objects(House.self)
        realmToken = outcomes.observe { _ in
            self.areas = outcomes
        }
    }
    
    func add(by title: String) {
        let area = House(title: title)
        attempt! SpaceStore.realm.write {
            SpaceStore.realm.add(area)
        }
    }
    
    func delete(_ area: House) {
        attempt! SpaceStore.realm.write {
            SpaceStore.realm.delete(area)
        }
    }
}

ContentView:

import SwiftUI

struct ContentView: View {
    @EnvironmentObject var retailer: SpaceStore
    
    var physique: some View {
        Button("Add") {
            retailer.add(by: "qwerty")
        }
        Divider()
        if let areas = retailer.areas {
            ForEach(areas) { area in
                HStack {
                    Textual content(area.title)
                    Button("Delete") {
                        retailer.delete(area)
                    }
                }
            }
            Divider()
            if let spaceItem = areas.first {
                ItemView(area: spaceItem)
            }
            Divider()
            ForEach(areas) { area in
                // RowView(area: area)
            }
        }
        
    }
}

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

ItemView:

import SwiftUI

struct ItemView: View {
    let area: House
    @EnvironmentObject var retailer: SpaceStore
    
    var physique: some View {
        HStack {
            Textual content(area.title)
            Button("Delete") {
                retailer.delete(area)
            }
        }
    }
}

#Preview {
    ItemView(area: House(title: "e"))
        .environmentObject(SpaceStore())
}

The doorway:

import SwiftUI

@fundamental
struct ForTestApp: App {
    @StateObject non-public var retailer = SpaceStore()
    
    var physique: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(retailer)
        }
    }
}

And the RowView is nearly the identical as ItemView besides title so I omitted it.


Right here is the bizarre issues throughout debugging:

  1. delete contained in the ContentView, it is okay
  2. delete within the ItemView for only one merchandise, it is okay
  3. delete within the RowView (inside ForEach), boooooom~

P.S.:

  1. Whereas I import the RowView, delete motion of 1&2 flip to be crashed too.
  2. Information was efficiently deleted though crashed after that. So I suppose that the crash was occurred throughout the rerendering.

Then I changed the @EnvironmentObject var retailer: SpaceStore with let retailer: SpaceStore in RowView and all the pieces appears to be working effectively.

Finally, I attempted to wrap the delete motion in a confirmationDialog like this:

.confirmationDialog("", isPresented: $ifShowSheetOfSettings) {
   Button("delete") {
      retailer.delete(area)
   }
}

It crashed once more.

I hope somebody may also help me to determine and level out the reason for the crash which is an important.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here