Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/satoshun/coroutineautodispose
Coroutine AutoDispose is an Kotlin Coroutine library for automatically disposal.
https://github.com/satoshun/coroutineautodispose
android coroutine kotlin
Last synced: about 2 months ago
JSON representation
Coroutine AutoDispose is an Kotlin Coroutine library for automatically disposal.
- Host: GitHub
- URL: https://github.com/satoshun/coroutineautodispose
- Owner: satoshun
- License: apache-2.0
- Created: 2018-12-23T02:17:40.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2021-05-30T00:33:39.000Z (over 3 years ago)
- Last Synced: 2023-08-01T22:38:32.682Z (over 1 year ago)
- Topics: android, coroutine, kotlin
- Language: Kotlin
- Homepage:
- Size: 334 KB
- Stars: 76
- Watchers: 3
- Forks: 5
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Coroutine AutoDispose
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.satoshun.coroutine.autodispose/autodispose/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.satoshun.coroutine.autodispose/autodispose)
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-CoroutineAutoDispose-green.svg?style=flat)](https://android-arsenal.com/details/1/7406)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](https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.LifecycleOwner).lifecycleScope:androidx.lifecycle.LifecycleCoroutineScope).It create a [CoroutineScope](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/) for automatically disposal with Android Architecture Component Lifecycle events.
```kotlin
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.
```kotlin
val job = launch { ... }
lifecycle.autoDispose(job)
```### Use with RecyclerView
CoroutineScope can be used from a itemView of RecyclerView.ViewHolder.
```kotlin
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
holder.itemView.autoDisposeScope.launch {
...
}
}
```## Download
```groovy
implementation 'com.github.satoshun.coroutine.autodispose:autodispose:${version}'
```## etc
This library referred [uber/AutoDispose](https://github.com/uber/AutoDispose).