经测试,内存泄漏 也存在于 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)
}
}
网友评论