Is there a way to make / imitate something like Scopes in Kotlin?
What I want to do is to cancel all running tasks when screen closed. So, imagine that using VIP pattern a task-scope is cretaed and Interactor keep strong reference to the scope.
When screen is closed, Interactor cancels the scope in deinit and task cancellation propagation happens. So finally all network requests, heavy data parsing & mapping taks are canceled.
Previously, we kept references to URLSession dataTasks and canceled them. It seems a more elegant and less error prone way can be done now.
Of course, such scope should be compatible with all existing and future kinds of Task and TaskGroups.
One more detail: Interactor and Presenter are classes for now. I also do some experiments to make them Actors.
So the solution should be suitable for both classes and Actors, but it is ok to have different solutions for classes and actors.
And interesting case is Actors. When task is created, strong reference to self is implicitly created in Task body.
So the trick with cancel-all tasks in deinit won't work, because deinit will not happen until last Task is finished and last strong reference disappear.
I want all Tasks / TaskGroups created in Interactor were connected or kept by some scope / pool by default.
When screen (in mobile App) is closed, all running tasks are canceled.
I will share some code later, thanks for the response.
You generally need to cancel only top level unstructured task (which you are more likely to create in UI), then if your interactors/presenters use structured concurrency, cancellation will automatically propagate to them. You can simply hold references to the tasks in your view and cancel once view disappears.
But code sample would definitely help to understand what’s you aiming for.