Continuing my standard weekend project of just playing with things, and I have a little inventory app. Basically something like
@Model
final class Room {
var id: UUID
var name: String
@Relationship(deleteRule: .cascade, inverse: \Item.room) var items: [Item]
}
@Model
final class Item {
var id: UUID
var name: String
@Relationship(deleteRule: .nullify) room: Room
}
Then in a SwiftUI view for each Room, I use another ItemsView
that constructs a query predicate based on the room ID that is passed in. And then on that, I've got a sheet to edit it, which is passed in @Bindable var item: Item
, and has a form to edit it, and cancel & save buttons. Standard stuff.
But if I edit the fields in the Item
, they get reflected immediately, which, ok, that's actually what I wanted so yay. But the "Save" button calls context.save()
while the "Cancel" button doesn't -- it calls context.rollback()
(and I have auto-save off).
And the problem I've got is: when I do that, the ItemsView
updates, in real time, but when I cancel, it doesn't update; I have to quit and relaunch the app to get that properly in sync.
The easiest change I can make, I presume, is to simply not use the passed in Item, but simply copy its values around to a new instance, but that won't update the item, so I'd have to delete it and re-insert it, or copy the fields back in the completion handler, or any number of things.
So my question really is: assuming what I just described makes sense, what's the proper way to deal with it?