Technically, if I dont cancel a CoroutineScope, bu...
# coroutines
u
Technically, if I dont cancel a CoroutineScope, but all its Jobs completed, then I don't leak memory, right?
d
If you're going to do that then use
GlobalScope
.
n
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
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
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
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
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