I just noticed that withAnimation wrapped changes to @Persisted properties of an @ObservableRealmObject are not being animated, contrary to doing the same thing on @Published properties of an @ObservedObject.
Is this known? Is there a fix / workaround / something else?
Hi @Pavel_Yakimenko !
I made a small example to demonstrate what I mean. Consider the following:
struct ContentView: View {
@ObservedObject var obObject: ObObject
@ObservedRealmObject var rlmObject: RlmObject
var body: some View {
if obObject.showSomething {
Text("ObObject before change")
.background(.red)
} else {
Text("ObOjbect after change")
.background(.blue)
}
if rlmObject.showSomething {
Text("rlmObject before change")
.background(.red)
} else {
Text("rlmObject after change")
.background(.blue)
}
Text("Change ObObject")
.onTapGesture {
withAnimation {
obObject.showSomething.toggle()
}
}
Text("Change RlmObject")
.onTapGesture {
withAnimation {
let realm = try! Realm()
try! realm.write {
$rlmObject.wrappedValue.showSomething.toggle()
}
}
}
}
}
with the Realm model and ObservableObject looking simple, like this:
class ObObject: ObservableObject {
@Published var showSomething: Bool = false
}
class RlmObject: Object, Identifiable {
@Persisted(primaryKey: true) var id = 1
@Persisted var showSomething: Bool = false
}
When you press on the “button” for the ObservableObject you will see the according field blend over, animating. When you do the same for the RealmObject, it will just snap over instantly, ignoring any animation (same when thawing the object first).
I would really love to hear your insights on this, and maybe a possible solution.