简体   繁体   English

RxJava实时数据错误处理

[英]RxJava to live data error Handling

So, in my android project I am using RxJava in the repository layer then I convert it to live data in the view Model, but I am having an exception when my stream emits an error. 因此,在我的android项目中,我在存储库层中使用RxJava,然后将其转换为视图模型中的实时数据,但是当我的流发出错误时,我遇到了异常。 It has something to do with this remark from the Android Documentation : 这与Android文档中的这一说法有关:

fromPublisher fromPublisher

LiveData<T> fromPublisher (Publisher<T> publisher) LiveData<T> fromPublisher (Publisher<T> publisher) LiveData<T> fromPublisher (Publisher<T> publisher)

Creates an Observable LiveData stream from a ReactiveStreams publisher. ReactiveStreams发布者创建一个Observable LiveData流。

When the LiveData becomes active, it subscribes to the emissions from the Publisher. LiveData变为活动状态时,它将订阅发布者的发射。

When the LiveData becomes inactive, the subscription is cleared. LiveData变为非活动状态时,将清除订阅。 LiveData holds the last value emitted by the Publisher when the LiveData was active. LiveData持有由出版商发出时的最后一个值LiveData活跃。

Therefore, in the case of a hot RxJava Observable, when a new LiveData Observer is added, it will automatically notify with the last value held in LiveData , which might not be the last value emitted by the Publisher. 因此,在热RxJava Observable的情况下,添加新的LiveData Observer ,它将自动通知LiveData保存的最后一个值,该值可能不是发布服务器发出的最后一个值。

Note that LiveData does NOT handle errors and it expects that errors are treated as states in the data that's held. 请注意, LiveData不会处理错误,并且期望将错误视为所保存数据中的状态。 In case of an error being emitted by the publisher, an error will be propagated to the main thread and the app will crash. 如果发布者发出错误,错误将传播到主线程,并且应用程序将崩溃。

How can I handle the stream error? 如何处理流错误? PS: I am using a Kotlin extension to convert to Live data PS:我正在使用Kotlin扩展程序转换为实时数据

fun <T> Observable<T>.toLiveData(backPressureStrategy: BackpressureStrategy =
                                                BackpressureStrategy.LATEST) :  LiveData<T> {
Log.d("RxJava",this.toString())
return LiveDataReactiveStreams.fromPublisher(this.toFlowable(backPressureStrategy))

As a newbie, i'm not sure about this solution. 作为新手,我不确定该解决方案。 But it works. 但这有效。 So, if it's bad, please comment to say why and provide an alternative. 因此,如果不好,请发表评论以说明原因并提供替代方案。

Use this : 用这个 :

data class DataWithStates<T>(
    val data: T? = null,
    val states: Throwable? = null
)

Sample for ViewModel : ViewModel的样本:

fun getUsers(): LiveData<DataWithStates<List<User>>> {
    return LiveDataReactiveStreams.fromPublisher(
        userRepository
            .getAll()
            .map { lstUser -> DataWithStates(lstUser) }
            .onErrorReturn { ex -> DataWithStates(states = ex) }
            .toFlowable(BackpressureStrategy.LATEST))
}

Maybe it's better to move .map and .onErrorReturn in the repository. 也许最好将.map和.onErrorReturn移动到存储库中。

Edit : alternatively, you can use a lambda to listen for error. 编辑:或者,您可以使用lambda侦听错误。

fun getUsers(onError: () -> Unit = {}): LiveData<List<User>> {
    return LiveDataReactiveStreams.fromPublisher(
        userRepository
            .getAll()
            .doOnError { onError() }
            .toFlowable(BackpressureStrategy.LATEST))
}

如果您使用的是实时数据(Android architecure组件)。最好使用其预定义的Oberserver(在建筑组件中)。否则,您完全只需要使用RX android clases

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM