Hi everyone, just a basic question that I'm still ...
# android
i
Hi everyone, just a basic question that I'm still not 100% shure: In your opinion what's the best CoroutineScope lifetime for activities and fragments? Currently I'm cancelling(the children) of the scope when a fragment is stopped and when an activity is destroyed. I used to cancel the scope when both were destroyed, but then I had some problems of running background operations not being cancelled when navigation to another fragment (fragment on backstack is stopped, not destroyed).
d
When it suits me. 😁 🧌
🧌 1
Sometimes I nest them when I need different levels of cancellation.
i
hm, I see, but what approach do you use more commonly?
d
I almost always have an
onDestroy
CoroutineScope
, which I use for loading stuff.
Sometimes I have a scope for
onStop
to cancel long running operations, like updating progress bars in the background (perhaps based on jobs from the
onDestroy
scope).
And in one particular case I have a nested scope for
onPause
, to cancel coroutines that hold on to a resource like a wake lock, media service or audio focus to unregister them.
But the
onDestroy
is the most common one.
i
hm, I see, it's something like having 3 scope objects on a base class, each with a different lifetime for a different use case
very interesting
l
I use a
createScope(activeWhile = aLifecycleState)
call. You can see its doc and implementation here: https://github.com/LouisCAD/Splitties/tree/master/modules/lifecycle-coroutines
g
fragment on backstack is stopped, not destroyed
@Icaro Temponi The best scope is Fragment lifecycleScope. Fragment has 2 flavors of scopes:
fragment.lifecycleScope
- which has lifecycle of Fragmnt object
fragment.viewLifecycleOwner.lifecycleScope
- which has lifecycle of Fragment’s view and cancelled automatically on View destroy (like in case of backstack or detaching)
and implementation here
Looks the same as existing official lifecycle-ktx extensions
m
I would think that depends on what you are doing inside that scope. If operation is tied to the fragment and can be cancelled without consequences (for example just observing some data) then I stop it onStop, otherwise onDestroy
g
than you should cancel observing operation in onStop, not a scope itself
m
yes, my bad I worded that wrongly
i
@Icaro Temponi I am not sure why you would need scope for Fragments in the first place 🤔 (can you give some concrete example?) The approach I follow is to have ViewModels for Activities and Fragmennts and starting from
2.1.0-rc01
the
lifecycle-viewmodel-ktx
exposes
viewModelScope
property for
ViewModel
g
Approach with ViewModel is completely different because essentially means that you should use different architecture for your app Also Fragment also has own lifecycle-kts
i
I just try to get a big picture here. Aren’t coroutines in Fragments mean we download data in fragments and thus we have no proper architecture? 🤔 (or rather no layer separation typical for bigger codebase)
m
you can perform non-downloading code in coroutines
for example handling permission callbacks could be much nicer in coroutines
just
val result = requestPermission(CAMERA)
👍 1
instead of juggling callbacks around
i
Yeah, I use coroutines to handle permissions, to launch some IO operations and api requests, these operations usually being tied to the UI (show/hide a progress, show itens loaded from remote api, collect flows, etc.), thus I think a scope that get's cancelled at onStop would fit better.
@igor.wojda I'm using the lifecycle scope, but exactly due to cancellation problems (navigate out of fragment, operations not being cancelled) with it I asked the question, because it's cancelled at onDestroy
I also use viewmodels + viewmodelScope, but there are some operations strictly tied to the fragment lifetime (e.g Room DB operations, some screens that should refresh the data everytime they are on foreground or observation/collection of some data using flow)
👍 1
l
About permissions: here's a ready to use little library to request them with coroutines: https://github.com/LouisCAD/Splitties/tree/master/modules/permissions Having a scope that cancels in
ondDestroyView
is fine here.
i
I use exactly this split to request permissions 🙂, tbh I use many of them, and I didn't think about using onDestroyView, good idea, thanks.
l
@Icaro Temponi Using
viewLifecycleOwner
default
lifecycleScope
should have exactly that effect.
i
Yeah, that should fit my use-case perfectly, didn't know this extension, thanks again @louiscad.