ViewModelScope is canceling a network request when user pops composition where ViewModel is injected. Is it possible to send a network request inside a Side-effects without interruption upon exiting the composition?
Use a wider scope for the operation. A common pattern used at the repository layer is to use
.async {}
in a scope owned by the repository to do work, and then await the result in a scope that may cancel when the UI doesn't care anymore. The work will still finish since it's not running in that UI scope, only awaiting a result was.
One thing that confuses me in compose is scopes as they are bound to a composition, activity or application. My case would mean that network call should be bound to the parent composition. To create the scope I would have to inject ViewModel into parent just for this purpose.
K Merle
10/12/2021, 6:12 PM
It also feels that I am breaking SRP, as my parent now has a responsibility of taking care of it's child network request lifetime.
a
Adam Powell
10/13/2021, 2:21 AM
A child may not "leak" work that lives longer than it does. To initiate work that outlives itself, the child must accept some object or dependency that can host that work.