RxBus 2

作者: ivotai | 来源:发表于2020-03-29 09:50 被阅读0次

经测试,内存泄漏 也存在于 RxBus 1 中。
网络请求造成的内存泄漏是短时间的。
而 RxBus 作为一个单例,会造成应用生命周期下的内存泄漏,改进如下。

object RxBus {

    private val subject: Subject<Any> = PublishSubject.create<Any>().toSerialized()

    fun post(any: Any) {
        subject.onNext(any)
    }

    fun <T> registerEvent(lifecycleOwner: LifecycleOwner, clazz: Class<T>, consumer: Consumer<T>) {
        subject.compose(disposeOnDestroy(lifecycleOwner))
            .ofType(clazz)
            .subscribe(consumer)
    }

}

相关文章

网友评论

      本文标题:RxBus 2

      本文链接:https://www.haomeiwen.com/subject/whjyuhtx.html