Rafal
11/02/2021, 7:47 AMListAdapter . Is onAttachedToRecyclerView/onDetachedFromRecyclerView valid place to manage the scope?
class CoroutineAdapter: ListAdapter<AdapterItem, AdapterViewHolder>() {
private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
coroutineScope.launch {
// stuff made here
}
}
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
super.onDetachedFromRecyclerView(recyclerView)
coroutineScope.coroutineContext.cancelChildren()
}
}louiscad
11/02/2021, 9:18 AMRafal
11/02/2021, 9:30 AMlouiscad
11/02/2021, 9:31 AMlouiscad
11/02/2021, 9:32 AMonDetachedFromRecyclerView can not be called. It'd be best to scope the coroutines to what contains the adapter so that you definitely know then the scope is cancelled.Rafal
11/02/2021, 9:35 AMrecyclerView.scrollEvents(): Flow inside the adapter, up until now we used RxBindings the same way, where we were disposing a Disposable in the onDetachedFromRecyclerViewlouiscad
11/02/2021, 9:37 AM