SwiftUI's withAnimation broken with @Persisted properties?

Hi there,

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?

Thanks in advance
L

Hi @Lila_Q
Is it possible to see the code?

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.

@Lila_Q
thank you. I can reproduce it. Let me check what I can do with it

1 Like

@Pavel_Yakimenko
Great, thanks. Looking forward to your results!

Hey @Pavel_Yakimenko were you able to find a solution / workaround for this?

Thanks in advance.

Hey @Pavel_Yakimenko , haven’t heard back yet, just checking if you could give any feedback about this.

Thanks in advance!