CoroutineAutoDispose

Introduction: Coroutine AutoDispose is an Kotlin Coroutine library for automatically disposal.
More: Author   ReportBugs   
Tags:

Maven Central

Coroutine AutoDispose is an Kotlin Coroutine library for automatically disposal.

Overview

Often, Coroutine subscriptions need to stop in response to some event (like a Activity#onStop()). In order to support this common scenario in Coroutine.

autoDisposeScope

This library provide a autoDisposeScope extension method. It can be used like a lifecycleScope.lifecycleScope:androidx.lifecycle.LifecycleCoroutineScope).

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.

Lifecycle.autoDispose(Job)

This Job an automatically disposal with Android Lifecycle events.

val job = launch { ... }
lifecycle.autoDispose(job)

Use with RecyclerView

CoroutineScope can be used from a itemView of RecyclerView.ViewHolder.

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
  holder.itemView.autoDisposeScope.launch {
    ...
  }
}

Download

implementation 'com.github.satoshun.coroutine.autodispose:autodispose:${version}'

etc

This library referred uber/AutoDispose.

Apps
About Me
GitHub: Trinea
Facebook: Dev Tools