如何解决Kotlin中的PublishSubject onError函数
因此,尝试订阅PublishSubject
时,我的Android应用程序(Kotlin)出现错误。
错误的解释很简单,但是,我尝试实现此onError
函数失败,并且我不确定该怎么做。
这是错误
The exception was not handled due to missing onError handler in the subscribe() method call. Further reading: https://github.com/ReactiveX/RxJava/wiki/Error-Handling | com.androidnetworking.error.ANError
这里是PublishSubject:
var positionSubject = PublishSubject.create<Location>()
当我订阅时(在订阅代码内给出错误):
compositeDisposable.add(
positionSubject.subscribe {
// do some actions here that causes Exception
}
)
在这里,我尝试以一种“不错”的方式修复它(没有用,在订阅时仍然崩溃):
compositeDisposable.add(
positionSubject
.onErrorReturn { t ->
Log.d("debug","EXCEPTION OCCURRED")
Location("")}
.subscribe {
// do some actions here that causes Exception
}
)
这是我最终要解决的事情,并且没有崩溃:
compositeDisposable.add(
positionSubject.subscribe {
try{
// do some actions here that causes Exception
}catch(e:Exception){
Log.d("debug","EXCEPTION OCCURRED $e")
}
}
)
我想知道如何甚至比在订阅中使用try / catch块更简洁的方式做到这一点。
解决方法
以下代码是kotlin
订阅PublishSubject
的方式
var positionSubject = PublishSubject.create<Location>()
positionSubject.subscribe({ location ->
},{ error ->
})
这应该工作正常。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。