kmm-production-sample icon indicating copy to clipboard operation
kmm-production-sample copied to clipboard

Handle coroutine cancelation on Activity lifecycle callbacks

Open lukasz-kalnik-gcx opened this issue 2 years ago • 4 comments

Support for canceling coroutines when Activity is destroyed is missing. FeedStore has hardcoded CoroutineScope(Dispatchers.Main) which is not bound to any lifecycle callbacks. This hardcoded coroutine scope also makes writing unit tests hard (it could be simply a constructor parameter).

lukasz-kalnik-gcx avatar Nov 30 '21 16:11 lukasz-kalnik-gcx

You are right. To inject CoroutineScope is good and right idea. But for our case it doesn't matter because FeedStore lives with Application lifecycle. And will be destroyed with app process. e.g. the FeedStore is needed not only for Activity but for background update service too.

terrakok avatar Nov 30 '21 22:11 terrakok

Ok, understand. Thank you for the explanation. Then as a feature request it would be helpful to also have an example of a screen which is canceling a coroutine when it closes. This is a quite common use case and would be good to see some best practices how it can be done from Android and iOS side.

lukasz-kalnik-gcx avatar Dec 01 '21 09:12 lukasz-kalnik-gcx

Is there any example that implements this idea?

Amrjyniat avatar Mar 31 '23 08:03 Amrjyniat

@Amrjyniat check my repo https://github.com/hoc081098/kmp-viewmodel/tree/master/sample

Sent from my 2201117TG using FastHub

hoc081098 avatar Mar 31 '23 10:03 hoc081098