Xcode 12.5, iOS 14.5, macOS 11.3
Let's say I have a simple Realm class like this in a SwiftUI app:
class Item: Object, ObjectKeyIdentifiable{
@objc dynamic var id = ""
@objc dynamic var name = ""
@objc dynamic var updated = Date()
}
I pass an object into a view using @ObservedRealmObject where I can edit its name property by binding it to a TextField view like this:
struct DetailView: View {
@ObservedRealmObject var item: Item
var body: some View{
TextField("Name...", $item.name)
}
}
When I edit the field, the name property updates as I type and the realm is updated in real-time.
But I also want to update the updated property with a new Date() timestamp whenever the object is modified. Since the write transaction is managed automatically by @ObservedRealmObject, I'm unclear on how to pull this off.
How can I change updated every time the name (or any other additional property) is changed?