r/iOSProgramming 3d ago

Discussion SwiftData kind of disrupts previously working animations

Hello, I haven't been able to actually have animations (upon object deletion, more specifically) work as they usually would if a variable was not wrapped with the Query macro (the macro used to fetch from SwiftData in SwiftUI). Has anyone experienced the same? If so, did you find a solution? I remember trying using the context (modelContext) itself but to no avail.

1 Upvotes

5 comments sorted by

1

u/PassTents 3d ago

Any specific code to share?

1

u/Over_Land2208 2d ago

not really, but I used to face this issue whenever I had:

@Query var something: [Something]

// (...)

ForEach(something) 
  { item in 
    RowView(item: item)
      .contextMenu { 
        Button("Delete", role: .destructive) {
          modelContext.delete(item) 
        } 
      } 
  }

1

u/Over_Land2208 2d ago

the row would just disappear even with a .transition modifier applied, for example.

1

u/ElectricKoolAid1969 1d ago

Have you tried either wrapping modelContext.delete(item) in withAnimation, or declaring the query as u/Query(animation: .default)? A .transition only describes the transition; it doesn’t itself create an animation transaction.