EffectTask.cancel(id:)
doesn't work if the reducer that starts the effect is different from the one cancelling it.
#2273
Replies: 1 comment
-
Hi @AndreaRomani, yeah this behavior is intended. It is what allows one to present the same feature multiple times (such as in a navigation stack) and not have the effects of one instance of the feature trample on the effects of another instance of the feature. And in general, it seems problematic for child features to be able to cancel parent effects like this. We even have changes coming to TCA in the somewhat near feature that will make the compartmentalization of reducer effects even a bit stronger. I think the better pattern for what you have described here is for the child feature to communicate to the parent that it wants something done, say via a delegate action, and then have the parent do the cancellation. I don't think we yet have any documentation about the delegate action pattern, but we did discuss it in this episode. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello! 👋 Opening this discussion following @stephencelis request on Slack 🙂
Since the recent navigation tools have been released (0.54) I noticed an unexpected behaviour with respect to effects cancellation.
The issue is that sometimes effects that are marked as
.cancellable(id: id)
aren't cancelled by a corresponding.cancel(id: id)
. In particular this happens if the reducer that starts the effect is different from the one that cancels it.By investigating a little bit I think I understood that this is related to the fact that effect cancellation requires not only that the cancellation id matches, but also that the
navigationIDPath
is the same.Even though I understand the reasons for this choice and I recognize its benefits for handling cancellation of effects in the context of navigation, I think this could be too restrictive in some cases, also because there's no indication of this fact in the API.
Suppose for example that I have a
Child
feature that exposes a start API to return its initial state to theParent
and start a long living effect.In this way any long living work started in
childEffect
wouldn't be torn down:Child
featureChild
featureChild
feature itself, by marking the effect as.cancellable
and using.cancel
, which instead is unexpected.As a consequence the following test - which passes successfully on
0.53
- fails because the child effect isn't torn down.Is this behaviour intended? Are there any best practices to handle this use case?
Beta Was this translation helpful? Give feedback.
All reactions