DataSources in Android Apps I have a datasource t...
# coroutines
l
DataSources in Android Apps I have a datasource that exposes a
SharedFlow
and performs network request every minute while there are active subscribers and emits the result. I don't want to perform network requests when the app is in the background. Should it be the responsibility of the datasource to stop performing network requests when the app goes into background, or should it be the responsibility of the clients of the data source to cancel their coroutines that collect from the datasource? If it's the responsibility of the clients, then one faulty client implementation can make the datasource to stay active ....
a
the datasource doesn't have the necessary signals to stop on its own, those signals are provided by the android entry points - activities, services, etc. That makes the lifecycle/scoping of each of those entry points your best signal to use by way of having clients collect in those scopes
Lifecycle[Owner].repeatOnLifecycle
is a helpful tool for this in cases that have a
LifecycleOwner
available