You need to observe a Book
changes via ObservedObject
wrapper, like
struct BookInfoView: View {
@Environment(.managedObjectContext) var context
@ObservedObject var book: Book
var body: some View {
print("refresh"); return
Group {
Button(action: onPublicToggled) {
Text(book.isPublic ? "private" : "public")
}
}
}
func onPublicToggled() {
context.perform {
book.isPublic.toggle()
try? context.save()
}
}
}
Note: @Environment(.managedObjectContext) var context
relates to context
itself, not to your CoreData objects.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…