pointfreeco / swift-identified-collections

A library of data structures for working with collections of identifiable elements in an ergonomic, performant way.
MIT License
531 stars 45 forks source link

Crash when used with rearrangeable SwiftUI List #46

Closed okla closed 1 year ago

okla commented 1 year ago

I'm using var items: IdentifiedArrayOf<MyType> with SwiftUI's List($items, editActions: .all) and once I rearrange any element in the list the app crashes with Precondition failed: Element identity must remain constant

okla commented 1 year ago

Using a workaround for now

List {
  ForEach($items) { ... }
    .onMove {
      items.move(fromOffsets: $0, toOffset: $1)
    }
}
stephencelis commented 1 year ago

@okla Can you provide a full reproduction of the original issue you were having for us to work with? Right now we don't really have much to go off of. We do have a test for move(fromOffsets:toOffset:), though:

https://github.com/pointfreeco/swift-identified-collections/blob/main/Tests/IdentifiedCollectionsTests/IdentifiedArrayTests.swift#L219

okla commented 1 year ago

@stephencelis Here's the full reproduction code

import SwiftUI
import IdentifiedCollections

struct Item: Identifiable {
  var id = UUID()
}

struct ContentView: View {
  @State var items: IdentifiedArray = [Item(), Item(), Item()]

  var body: some View {
    List($items, editActions: .all) { item in
      Text("\(item.wrappedValue.id)")
    }
  }
}

@main
struct testApp: App {
  var body: some Scene {
    WindowGroup {
      ContentView()
    }
  }
}
stephencelis commented 1 year ago

@okla Thanks! Can you try this PR's branch and see if it fixes the issue for you?

https://github.com/pointfreeco/swift-identified-collections/pull/48

okla commented 1 year ago

@stephencelis Works fine now!