Coroutine AutoDispose is an Kotlin Coroutine library for automatically disposal.
Often, Coroutine subscriptions need to stop in response to some event (like a Activity#onStop()). In order to support this common scenario in Coroutine.
This library provide a autoDisposeScope extension method. It can be used like a lifecycleScope.
It create a CoroutineScope for automatically disposal with Android Architecture Component Lifecycle events.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
// automatically dispose when onDestroy
autoDisposeScope.launch {
...
}
}
override fun onResume() {
// automatically dispose when onPause
autoDisposeScope.launch {
...
}
}
}
It can also be uses with Fragment and View.
This Job an automatically disposal with Android Lifecycle events.
val job = launch { ... }
lifecycle.autoDispose(job)
CoroutineScope can be used from a itemView of RecyclerView.ViewHolder.
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
holder.itemView.autoDisposeScope.launch {
...
}
}
implementation 'com.github.satoshun.coroutine.autodispose:autodispose:${version}'
This library referred uber/AutoDispose.