SwiftUI ForEach.onDelete properly animates but then resets

Viewed 12

Trying to figure out why my ForEach loop isn’t updating. I have the model marked as ObservedObject and have done everything I could to make sure the updates were happening. I even saw that the model was being updated while printing.

class Model {
    var array: [Int]
}
…
struct ModelView: View {
    @ObservedObject var model: Model
var body: some View {
    List {
         ForEach(model.array.indices, id:\.self) { index in
…
        }.onDelete(perform: delete)
    }
}

The row is animating and acting like it is deleting and does delete in the model, however the deleted row animates back in and the original data set is shown!

1 Answers

I figured it out. I needed to make the array in my model object with the @Published property wrapper. Doing that fixed everything!

Related