-
I'm currently doing some navigation refactoring and starting to use the destination pattern with PresentationState. There is a child feature that is presented as a sheet. The child feature has to somehow notice that it is being dismissed to do some work such as closing network connections or stopping audio playback.
As the child feature state wasn't optional, we could send an action on disappear of the child view, so the child reducer could do the cleanup, when the sheet was pulled down:
When using PresentationState, this is obviously not possible as we get an error:
My current workaround is to move the cleanup code to a static function and call it from the parent reducer on dismiss:
That is a problem, because we have to remember to add that dismiss handling everywhere the ChildFeature can be presented. The cleanup has to happen inside the child instead, but I'm missing how the child could observe dismissal before it's too late. I hoped the docs would have a solution, but didn't find anything. It would be highly appreciated if anyone could point me in the right direction. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @piojost, when using the play: { url in
await withTaskCancellationHandler {
await player.play()
} onCancel: {
player.stop()
}
} This does require your dependency to be a little more deeply ingrained with Swift concurrency, but that has other benefits too. For example, in the above code, the This can work with any kind of effect too. Do you think that will help in your situation? |
Beta Was this translation helpful? Give feedback.
Hi @piojost, when using the
ifLet
operator, all child effects are automatically canceled when child state isnil
'd out, and so you can use this knowledge in your dependency to do any necessary tear down. For example, aplay
endpoint on anAudioPlayerClient
could listen for cancellation to automatically stop:This does require your dependency to be a little more deeply ingrained with Swift concurrency, but that has other benefits too. For example, in the above code, the
player.play
needs to beasync
and suspend for as long as the audio is playing. That allows you to tie the…