Let’s say I have a class like this:
@Model
final class MyItem: Identifiable {
@Attribute(.unique) var id: String
var name: String
init(id: String, name: String) {
self.id = id
self.name = name
}
}
How can I listen for changes to this object in the database, not just the local instance.
class ViewModel {
private var cancellables = Set<AnyCancellable>()
init(myItem: MyItem) {
// Something like this
item.objectWillChange.sink { completion in
print("something")
} receiveValue: { som in
print("something new")
}
.store(in: &cancellables)
}
}
I’ve tried conforming MyItem
to ObservableObject
. And using objectWillChange
. This allows be to use objectWillChange.sink
, but when I make changes and save, I don’t see any changes. I know the changes are persisting to the DB because I see the changes stick between builds of the app.
I’ve tried adding @Published to var name: String
, but this doesn’t compile because @Model doesn’t allow property wrappers.