Replies: 1 comment 1 reply
-
Hi @wtruppel! Thanks for sharing your predicament. You might be interested in this recent discussion on navigation stacks if you haven't already read it: #1140 We also have some early, but promising experiments with the APIs, but we're pretty focussed on the Concurrency Beta at the moment, so we probably won't gave time to return to them for a little bit. There's also a new |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone :)
Our team is having some difficulties using the Composable Architecture to present a navigation stack in a way that lets each scene properly set itself up and tear itself down, and we could use some help and suggestions from more experienced folks.
Let me start by saying that our difficulties are not with navigation itself (yes, we ran into those too, thanks to
NavigationLink
, but we found a solution to those). Rather, they have to do with properly setting up and tearing down scene states, because our scene states have to set up and clean up resources external to the app and we can't achieve those in state initialisers because those actions are asynchronous and best handled by invoking the environment.In a nutshell, when a parent scene is about to present a child scene, it needs to tear down its current navigation stack (since it could be presenting a child scene other than the one it’s about to present). This process must happen recursively for all scenes in the navigation stack, until the leaf scene, which simply cleans itself up. The recursion then unravels and the original parent scene can then safely present whatever child scene it was going to present originally (at which point it needs to create the child state and then ask it to do any further setup that can't be done in the state initialiser).
It’s conceptually simple but the details are complicated because the only way scenes communicate with one another is by firing actions which the scene reducers consume and process.
So, when a parent scene is about to present
childA
, the parent reducer'shandler tells its currently presented child, if any, to tear down its part of the navigation stack. More specifically, the parent calls a function on the child state - which is known at that time - to tear down the navigation stack rooted at it). This usually involves some asynchronous calls so that function takes the child environment as an argument and returns an
Effect
:After tearing down the navigation stack rooted on itself, the child fires an action that essentially says “I’m done”. That action is intentionally ignored by the child reducer but percolates up to the parent reducer, which then must pick up from where it was and continue on.
(in
childB
's reducer)(in the parent reducer)
As a result, the communication between scenes litters the reducers with back-and-forth actions whose only purpose is to tell another scene’s reducer that some other action was completed. Moreover, the logic for processing an action is often interrupted and split in two because it needs to wait for the child to do some cleanup. And the state has to keep track of which child presentation is currently "in flight".
None of this is business logic!
Incidentally, we already had a similar process going on to set up and to clean up scenes but we didn’t clear the navigation stack prior to every presentation, and that caused issues with deep-linking. When we got a deep link, we were simply presenting the new navigation stack without cleaning up any prior one, essentially obliterating that current navigation stack, so none of its scenes got to clean themselves up.
(before we discovered issues with deep linking)
The above setup and cleanup still needs to happen, so the code just above needs to be integrated with the code involving
clearNavigationStack
.So, to summarise, we have an explosion of actions and corresponding reducer handlers, and other ancillary state, just to signal back and forth between scenes that setting and cleaning scenes up, and tearing down the navigation stack, have started or are completed.
There has to be a better way and we would be grateful for any suggestions that can alleviate this situation.
Many thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions