0

因此,当我尝试订阅PublishSubject.

错误解释非常简单,但是,我尝试实现这个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

这里是发布主题:

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 块更清洁的方式来实现这一点,如果可能的话。

4

1 回答 1

1

以下代码是kotlin订阅的方式PublishSubject

var positionSubject = PublishSubject.create<Location>()
positionSubject.subscribe({ location ->
    
}, { error ->
    
})

这应该可以正常工作。

于 2020-09-20T08:03:00.580 回答