Coroutine AutoDispose is an Kotlin Coroutine library for automatically disposal.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci only debug unit test Dec 28, 2018
app fix format Dec 29, 2018
autodispose use ActivityScenarioRule Dec 31, 2018
buildSrc update test lib Dec 31, 2018
gradle enable jacoco Dec 28, 2018
.gitignore first Dec 22, 2018
LICENSE add license Dec 23, 2018
README.md add codecov badge Dec 28, 2018
build.gradle enable jacoco Dec 28, 2018
debug.keystore first Dec 22, 2018
gradle.properties 0.2.0-SNAPSHOT Dec 28, 2018
gradlew first Dec 22, 2018
gradlew.bat first Dec 22, 2018
settings.gradle add first libs module Dec 23, 2018

README.md

Coroutine AutoDispose

CircleCI Maven Central codecov Android Arsenal

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.

LifecycleAutoDisposeInterceptor(Lifecycle)

LifecycleAutoDisposeInterceptor can use with CoroutineScope. It create a CoroutineInterceptor for automatically disposal with AAC lifecycle events.

abstract class BaseActivity : AppCompatActivity(),
  CoroutineScope {

  override val coroutineContext get() = Dispatchers.Main +
      LifecycleAutoDisposeInterceptor(this) // or autoDisposeInterceptor()
}

class MainActivity : BaseActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    // automatically dispose when onDestroy
    launch {
      ...
    }
  }

  override fun onResume() {
    // automatically dispose when onPause
    launch {
      ...
    }
  }
}

Lifecycle.autoDispose(Job)

This Job an automatically disposal with Android Lifecycle events.

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

ViewAutoDisposeInterceptor(Lifecycle)

ViewAutoDisposeInterceptor can use with CoroutineScope. It create a CoroutineInterceptor for automatically disposal with View attach/detach events.

class MainView(context: Context) : View(context), CoroutineScope {
  override val coroutineContext
    get() = Dispatchers.Main +
      ViewAutoDisposeInterceptor(this) // or autoDisposeInterceptor()
  ...
}

Donwload

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

etc

This library referred uber/AutoDispose.