Problem when using TCA combined with iOS 16 NavigationStack #1692
-
Hello! I'm fairly new in the SwiftUI and TCA domain, so bear with me. I like the thoughts behind it and figured, let's test it out with an iOS 16 demo app! As I've seen the latest video's, I wanted to split up navigation/coordination from the actual view's as as much as possible, but also as easy as possible. I couldn't really find my specific issue within the current discussions. I'm doing this by creating a RouterView which returns the views that are requested by the navigationDestination. Whenever I do an async task (i.e. using a Clock, thanks for those video's too) within the reducer, it seems that the navigationStack is refreshed and loses state completely from both the previous View when navigating back, as the View where I try navigating towards. It might be a very core SwiftUI thing I'm doing wrong. Although it does work without TCA it seems. Any tips on what might be wrong in my example project? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
The problem is in this line. This creates a whole new store from scratch every time the view is recreated, which causes any state changes to be lost and all effects to be cancelled. This is why in TCA, when creating child features, one scopes an existing store to pass down to the child instead of constructing a whole new one from scratch. However, this store-scoping style does not immediately play nicely with So, essentially more work needs to be done to build the tools for TCA to be able to take advantage of the new iOS 16 navigation tools. That has been our focus for many months (really over a year), and it's the topic of our next series of episodes after the one we are starting next week. We will show you can take advantage of |
Beta Was this translation helpful? Give feedback.
-
Right, to simplify and summarise, it's not really possible to get TCA working per feature/screen within a SwiftUI only app? The only way to use it, is to apply the architecture fully throughout the whole app. Defining an application reducer with child reducers? There is no use for me applying said scoped pattern throughout the app using the I'm not sure how Apple thought about how to apply the Looking forward to next updates regarding navigation 👍🏻 Edit: |
Beta Was this translation helpful? Give feedback.
The problem is in this line. This creates a whole new store from scratch every time the view is recreated, which causes any state changes to be lost and all effects to be cancelled.
This is why in TCA, when creating child features, one scopes an existing store to pass down to the child instead of constructing a whole new one from scratch.
However, this store-scoping style does not immediately play nicely with
NavigationStack
. And really this is a vanilla SwiftUI problem too. As we showed in the most recent episode, if you want be able to observe what is happening in each screen of a stack you are forced to put a full-blown observable object in each case of the destination enum, which then…