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.25k stars 1.42k forks source link

StackState and NavigationStack occasionally get out of sync under specific circumstances #2806

Closed djangovanderheijden closed 7 months ago

djangovanderheijden commented 7 months ago

Description

I've run into an issue where occasionally, items appended to the StackState are not pushed onto SwiftUI's NavigationStack. It's a bit of a tricky one as the problem seems to be timing related and can happen more or less often depending on your project setup. In my real-life project the problem occurs far more often, probably due to some unfortunate timing.

Note that I am unsure whether this is a TCA or SwiftUI problem. I have not been able to reproduce it in vanilla SwiftUI but the behavior is flaky enough that I can't be certain.

Checklist

Expected behavior

I expect StackState and NavigationStack to always remain in sync under normal circumstances.

Actual behavior

The recording below is the attached gist (see steps to reproduce) running on my iPhone 13 Mini. I tap the center button to append an item to the StackState, then tap the back button to pop it again. The StackState's collection of IDs is displayed at the bottom of the screen.

As you can see, the NavigationStack occasionally doesn't navigate to the new destination even though the StackState is updated. (StackElementID 40 at 0:08 and StackElementID 53 at 0:32)

https://github.com/pointfreeco/swift-composable-architecture/assets/60142790/deed4ed0-c46c-4620-80f9-89905b0c228e

Steps to reproduce

I've set up an example project that showcases the issue: https://gist.github.com/djangovanderheijden/ec70561f9483c88cff7b453dcc742e79

Sorry for the bulkiness, this is about as small as I've been able to get the reproducible example. I've added some comments in important spots.

Also note that as the issue is seemingly timing related, the behavior on your device may differ. I have not been able to reproduce this issue in the simulator.

The Composable Architecture version information

1.8.0

Destination operating system

17.2.1

Xcode version information

15.2 (15C500b)

Swift Compiler version information

swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: arm64-apple-macosx14.0
mbrandonw commented 7 months ago

Hi @djangovanderheijden, I'm not really sure this is a TCA bug. It seems far more likely to be a vanilla SwiftUI bug, and I would recommend trying to reproduce in vanilla SwiftUI.

I'm going to convert this to a discussion for now and please do share any findings you come across in the future.