johnpatrickmorgan / FlowStacks

FlowStacks allows you to hoist SwiftUI navigation and presentation state into a Coordinator
MIT License
854 stars 66 forks source link

Background thread publishing changes when using `RouteSteps` #27

Closed utahwithak closed 2 years ago

utahwithak commented 2 years ago

This issue doesn't appear to happen when using the binding modifiers. I'm not sure why. But when using:

        RouteSteps.withDelaysIfUnsupported(self, \.routes) {
            $0.goBackToRoot()
        }

if there are more than one steps it will print out the following:

FlowStacksApp[55359:4275326] [SwiftUI] Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates.

It appears that inside scheduleRemainingSteps when it does a Task.sleep when it comes back it will no longer be on the main thread and all remaining operations will execute on a random background thread pool thread.

Marking scheduleRemainingSteps as @MainActor solves this issue, though may have other implications.

This was tested on iOS 15 and 15.5 on simulators. Same thread changing behavior can be seen with a playground as well,

DavidKmn commented 2 years ago

Good catch, yes annotating the scheduleRemainingSteps to run on the main actor would be the correct solution. Will submit a PR shortly and attach to the thread.

DavidKmn commented 2 years ago

PR created #28

johnpatrickmorgan commented 2 years ago

Thanks @utahwithak for raising this issue and @DavidKmn for the quick fix!

johnpatrickmorgan commented 2 years ago

Merged and released in V0.2.1.