rxjava取消订阅--利用kotlin的扩展方法和android的lifecycle库实现
lifecycle库在27之后的support包包含不需要单独导入之前的版本可以前往Android官网查看文档
下面直接上代码:
扩展方法封装
import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.LifecycleObserver
import android.arch.lifecycle.OnLifecycleEvent
import io.reactivex.disposables.Disposable
import java.util.*
import kotlin.collections.HashMap
private val map = Collections.synchronizedMap(HashMap<String, BaseLifecycleObserver>())
fun Disposable.destroy(onceKey: String? = null, lifecycle: Lifecycle) {
lifecycle.addObserver(BaseLifecycleObserver(this, lifecycle, onceKey, Lifecycle.Event.ON_DESTROY))
}
fun Disposable.stop(onceKey: String? = null, lifecycle: Lifecycle) {
lifecycle.addObserver(BaseLifecycleObserver(this, lifecycle, onceKey, Lifecycle.Event.ON_STOP))
}
fun Disposable.pause(onceKey: String? = null, lifecycle: Lifecycle) {
lifecycle.addObserver(BaseLifecycleObserver(this, lifecycle, onceKey, Lifecycle.Event.ON_PAUSE))
}
class BaseLifecycleObserver(val disposable: Disposable,
private val lifecycle: Lifecycle,
private val onceKey: String? = null,
private val event: Lifecycle.Event = Lifecycle.Event.ON_DESTROY) : LifecycleObserver {
init {
if (onceKey != null) {
val observer = map.remove(onceKey)
map[onceKey] = this
if (observer != null) {
observer.disposable.dispose()
lifecycle.removeObserver(observer)
}
}
lifecycle.addObserver(this)
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onStop() {
if (event == Lifecycle.Event.ON_STOP) {
clean()
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
if (event == Lifecycle.Event.ON_DESTROY) {
clean()
}
}
private fun clean() {
disposable.dispose()
if (onceKey != null)
map.remove(onceKey)
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun onPause() {
if (disposable.isDisposed) {
lifecycle.removeObserver(this)
if (onceKey != null)
map.remove(onceKey)
}
if (event == Lifecycle.Event.ON_PAUSE) {
clean()
}
}
}
这里主要声明了三个方法扩展destory、stop、pause
参数: oncekey ,唯一的key 如果重复的话会直接取消之前的disposeable 如果为null 则不处理
参数:lifecycle ,被绑定到的声明周期对象,通常由 activity 或者 fragment 提供,也可以自定义。
destory stop pause 三个扩展方法,表示在相应的生命周期 取消订阅。
BaseLifecycleObserver:这个应该叫 参数 Lifecycle 的 声明周期观察者 。内部通过传入的参数判断何时取消订阅。
不想码字了直接上代码吧。并不复杂只是一点点小技巧
使用(activity中也可以 或者如果有自定义的lifecycleowner 对象也可以)
import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.Log
import android.view.View
import io.reactivex.Observable
import sjj.fiction.util.destroy
import sjj.fiction.util.pause
/**
* Created by SJJ on 2017/10/7.
*/
open class BaseFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//当fragment 声明周期调用destroy 时会自动取消订阅
Observable.just("a").subscribe {
Log.e("a", "a")
}.destroy(lifecycle = lifecycle)
//重复调用自动取消上一次,某些时候很有用
Observable.just("a").subscribe {
Log.e("a", "a")
}.destroy("onceaaakey",lifecycle = lifecycle)
//当fragment 声明周期调用pause 时会自动取消订阅
Observable.just("a").subscribe {
Log.e("a", "a")
}.pause(lifecycle = lifecycle)
}
}
再进一步的封装
在activity或者fragment中添加扩展方法
import android.support.v4.app.Fragment
import io.reactivex.disposables.Disposable
import sjj.fiction.util.destroy
import sjj.fiction.util.pause
import sjj.fiction.util.stop
/**
* Created by SJJ on 2017/10/7.
*/
open class BaseFragment : Fragment() {
fun Disposable.destroy(onceKey: String? = null) {
destroy(onceKey, lifecycle)
}
fun Disposable.stop(onceKey: String? = null) {
stop(onceKey, lifecycle)
}
fun Disposable.pause(onceKey: String? = null) {
pause(onceKey, lifecycle)
}
}
这里在fragment中添加扩展方法之后在fragment中使用更加方便
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Observable.just("a").subscribe {
}.destroy()
Observable.just("a").subscribe {
}.pause()
}
网友评论