Technically, if I dont cancel a CoroutineScope, but all its Jobs completed, then I don't leak memory, right?
d
Dominaezzz
12/28/2021, 2:28 AM
If you're going to do that then use
GlobalScope
.
n
Nick Allen
12/28/2021, 3:02 AM
If there's no references to it, it'll get GC'd. Children don't even have to be complete, they just need to be unreferenced (not running and no reference to the Continuation so it can never be resumed).
u
ursus
12/28/2021, 4:21 AM
Cool then it should be fine. Im trying to add diffing to android recyclerview adapter, via coroutines as impl. detaild and it needs a scope, but Adapter had no lifecycle, and I cannot be bothered to call some public adapter.clear. So it should mean I only leak it temporarily, until diff finishes, which is only few ms.
n
Nick Allen
12/28/2021, 4:51 AM
Plenty of simple alternatives like passing in the activity's
CoroutineScope
, or creating/cancelling based on `onAttachedToRecyclerView`/`onDetachedFromRecyclerView`. If it's just to be lazy ... then sure, it'll GC (you can use profiler to be certain). And if you never cancel, then you might as well use
GlobalScope
(It'll make it clearer that you aren't cancelling).
l
louiscad
12/28/2021, 10:40 AM
If what you're looking for is running coroutines while a position/item is bound to a viewholder, I recommend you to check out this library made by one of the RecyclerView maintainers himself: https://github.com/yigit/suspend-bind
s
stojan
12/29/2021, 9:50 AM
Cool then it should be fine. Im trying to add diffing to android recyclerview adapter, via coroutines as impl. detaild and it needs a scope, but Adapter had no lifecycle
the adapter should use the lifecycle of the Activity/Fragment that contains it.... you can pass the lifecycle/scope in the constructor
you also might wanna check
ListAdapter
that already does the diffing on a background thread