Diposer implementation
| import android.os.Bundle | |
| import androidx.appcompat.app.AppCompatActivity | |
| import androidx.lifecycle.Lifecycle | |
| import androidx.lifecycle.LifecycleObserver | |
| import androidx.lifecycle.LifecycleOwner | |
| import androidx.lifecycle.OnLifecycleEvent | |
| import io.reactivex.Flowable | |
| import io.reactivex.disposables.Disposable | |
| import io.reactivex.schedulers.Schedulers | |
| import timber.log.Timber | |
| import java.util.concurrent.TimeUnit | |
| class LaunchActivity : AppCompatActivity() { | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_launch) | |
| } | |
| override fun onResume() { | |
| super.onResume() | |
| Timber.i("create single") | |
| Flowable.range(0, 10_000) | |
| .delay(100, TimeUnit.MILLISECONDS) | |
| .subscribeOn(Schedulers.io()) | |
| .subscribe({ | |
| Timber.i("counted $it") | |
| }, {}) | |
| .dispose(Lifecycle.Event.ON_PAUSE) | |
| } | |
| fun Disposable.dispose(filter: Lifecycle.Event) { | |
| lifecycle.addObserver(Disposer(this, filter)) | |
| } | |
| @Suppress("UNUSED", "UNUSED_PARAMETER") | |
| open class Disposer( | |
| private val disposable: Disposable, | |
| private val filter: Lifecycle.Event): LifecycleObserver { | |
| @OnLifecycleEvent(Lifecycle.Event.ON_ANY) | |
| fun onAny(source: LifecycleOwner, event: Lifecycle.Event) { | |
| if (event != filter) return | |
| if (!disposable.isDisposed) disposable.dispose() | |
| Timber.i("disposed") | |
| } | |
| } | |
| } |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment
Hide comment
sellmair
commented
Oct 2, 2018
•
|
That is what I am talking about! 26 LOC implementation |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment
Hide comment
sellmair
Oct 2, 2018
Maybe you might want to remove the Disposer from the lifecycle with removeObserver once the internal Disposable is disposed, because then the Disposer is not needed anymore
sellmair
commented
Oct 2, 2018
|
Maybe you might want to remove the |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That is what I am talking about! 26 LOC implementation☺️ 👍