How to handle error states with LiveData?

后端 未结 7 617
暗喜
暗喜 2020-12-07 14:10

The new LiveData can be used as a replacement for RxJava\'s observables in some scenarios. However, unlike Observable, LiveData has no callback for

7条回答
  •  星月不相逢
    2020-12-07 14:56

    In one of Google's sample apps for Android Architecture Components they wrap the LiveData emitted object in a class that can contain a status, data, and message for the emitted object.

    https://github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/main/java/com/android/example/github/vo/Resource.kt

    With this approach you can use the status to determine if there was an error.

提交回复
热议问题