Skip to content

Coroutine AutoDispose is an Kotlin Coroutine library for automatically disposal.

License

Notifications You must be signed in to change notification settings

satoshun/CoroutineAutoDispose

Repository files navigation

Coroutine AutoDispose

Maven Central 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.

autoDisposeScope

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.

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.

About

Coroutine AutoDispose is an Kotlin Coroutine library for automatically disposal.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages