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
11.91k stars 1.37k forks source link

NavigationStack path doesn't retain correct detail view when reordering a list with NavigationLinks #3183

Closed stickeejosh closed 3 weeks ago

stickeejosh commented 3 weeks ago

Description

When using a NavigationLink inside a ForEach in a NavigationStack, if the ordering of the list changes whilst a detail view is presented, the detail view changes its content to the element in the ForEach that is at the same index, rather than retaining the same view. This doesn't happen with Vanilla SwiftUI or when appending to the path in a Reducer from e.g. a button tap.

Checklist

Expected behavior

The views in a NavigationStack path shouldn't change with list ordering or underlying ForEach NavigationLinks.

The following project shows this doesn't happen for Vanilla SwiftUI. ReorderTest.zip

Actual behavior

If list ordering of NavigationLinks changes in the ForEach of the root view in the stack then detail views change their content. Here I have edited SyncUps to sort sync ups based on title when tapping 'Start Meeting' rather than starting the meeting (to simply show the unexpected behaviour). In the video attached, when I edit Sync Up "Test 1" (3rd element in the list) to "Test 3" and trigger the sort of the list, the item in the detail view changes to "Test 2" (3rd element in the updated list).

https://github.com/pointfreeco/swift-composable-architecture/assets/156822019/4029aba9-d442-4e70-b7ee-a83de85baa19

Steps to reproduce

In the sync up example app, change

state.path.append(.record(RecordMeeting.State(syncUp: sharedSyncUp)))

in AppFeature.swift to

state.syncUpsList.syncUps.sort(by: { $0.title < $1.title })

The Composable Architecture version information

1.11.1

Destination operating system

iOS 17

Xcode version information

Xcode 15.4

Swift Compiler version information

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: x86_64-apple-macosx14.0
mbrandonw commented 3 weeks ago

Hi @stickeejosh, thanks for the report! This was definitely a bug and will be fixed by #3187.

stickeejosh commented 3 weeks ago

Great. Thanks for the quick fix.