I have a view where realm object can be updated, and when I do that the view dismisses, and that’s unwanted behavior, after the object is updated the view should still be on screen. What could be the problem here and possible solution?
struct CardView: View {
@ObservedRealmObject var card: Card
@State private var frontText: String = ""
@State private var backText: String = ""
@EnvironmentObject private var realmManager: RealmManager
var body: some View {
VStack {
TextField("", text: $frontText)
Divider()
TextField("", text: $backText)
}
.onAppear {
frontText = card.front
backText = card.back
}
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button {
do {
try realmManager.realm?.write {
guard let card = card.thaw() else { return }
card.front = frontText
card.back = backText
}
} catch {
print(error.localizedDescription)
}
} label: {
Text("Update")
}
}
}
}
}
How the view opens:
List {
ForEach(deck.cards) { card in
NavigationLink {
CardView(card: card)
.environmentObject(RealmManager())
} label: {
Text(card.front)
}
}
}
I have found a solution:
NavigationView {
// ...
}
.navigationViewStyle(.stack)
I used this modifier on NavigationView and it did the trick.