pointfreeco / swift-composable-architecture

A library for building applications in a consistent and understandable way, with composition, testing, and ergonomics in mind.
https://www.pointfree.co/collections/composable-architecture
MIT License
12.5k stars 1.46k forks source link

ForEach<_StoreCollection<String, State, Action>, ObjectIdentifier, SingleTaskView>: the ID ObjectIdentifier(0x0000600003fe8b40) occurs multiple times within the collection, this will give undefined results! #3055

Closed zhangpeibj01 closed 5 months ago

zhangpeibj01 commented 5 months ago

Description

I have studied the latest code of ToDos and tried to improve my project. But the unexpected issue occured. I am not sure if I did something wrong. I replace Array<Task> with IdentifiedArrayOf<Task> and replace

ForEachStore(
  store.scope(
    state: \.tasks,
    action: \.tasks
  )
) { (store) in
   SingleTaskView(store: store)
}

with

List {
      ForEach(store.scope(state: \.tasks, action: \.tasks)) { store in
        SingleTaskView(store: store)
      }
} 

Then the issue occured.

Checklist

Expected behavior

Can someone help me, Thanks

Actual behavior

ForEach<_StoreCollection<String, State, Action>, ObjectIdentifier, SingleTaskView>: the ID ObjectIdentifier(0x0000600003fe8b40) occurs multiple times within the collection, this will give undefined results!

Steps to reproduce

I tried to reassgin the value of state.todos.

The Composable Architecture version information

1.10.0

Destination operating system

macos 14.3

Xcode version information

Version 15.2 (15C500b)

Swift Compiler version information

Swift version 5.9.2
zhangpeibj01 commented 5 months ago

I logged state.todos and there were no identical ids.

stephencelis commented 5 months ago

@zhangpeibj01 In the upgrade guide we note that the 1:1 migration requires you to explicitly select the id from store state, otherwise ForEach will take the child store's object identifier:

https://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/migratingto1.7#Replacing-ForEachStore-with-ForEach

So you should update your code for a true comparison:

 ForEach(
-  store.scope(state: \.tasks, action: \.tasks)
+  store.scope(state: \.tasks, action: \.tasks), id: \.state.id
 ) { store in
   SingleTaskView(store: store)
 }

I'm going to convert this to a discussion since it doesn't appear to be a bug with the library, but if you do think you've encountered a bug, please provide a sample project that reproduces the issue. Without it we won't be able to troubleshoot the problem.