我没有从Android架构组件中获得在Android和LiveData中使用RxJava的理由。如果以代码的形式解释用例和两者之间的差异,以及解释两者之间差异的示例示例,将会非常有帮助。
当前回答
关于最初的问题,RxJava和LiveData都能很好地互补。
LiveData在ViewModel层上表现出色,它与Android生命周期和ViewModel紧密集成。RxJava在转换方面提供了更多的功能(正如@Bob Dalgleish所提到的)。
目前,我们在数据源和存储库层使用RxJava,并在ViewModels中转换为LiveData(使用LiveDataReactiveStreams)(在将数据暴露给活动/片段之前)-非常满意这种方法。
其他回答
我的简单回答是不要使用RxJava。这是一种过于复杂和滥用的方式。对于所有使用RxJava的项目来说,它们都很难维护和调试。RxJava是异步的,使用线程来分派请求,一般来说这是完全不必要的,如果需要的话,Kotlin协程在99%的情况下做得更好。
LiveData部分等于Rx Subject或SharedRxObservable LiveData管理订阅的生命周期,但Rx主题 订阅应该手动创建和处理 LiveData没有终止状态,但Rx Subject有OnError 和oncomplete
比较LiveData和RxJava就像比较苹果和水果沙拉。
比较LiveData和contenttobserver,你是在比较苹果和苹果。LiveData有效地替代了contenttobserver的生命周期。
将RxJava与AsyncTask或任何其他线程工具进行比较,就像将水果沙拉与橙子进行比较一样,因为RxJava帮助的不仅仅是线程。
LiveData和RxJava有很多不同之处:
LiveData is not a STREAM while in RxJava everything (literally everything) is a STREAM. LiveData is an observable data holder class. Unlike a regular observable, LiveData is lifecycle-aware, meaning it respects the lifecycle of other app components, such as activities, fragments, or services. This awareness ensures LiveData only updates app component observers that are in an active lifecycle state. LiveData is synchronous, So you can't execute a chunk of code (network call, database manipulation etc.) asynchronously using just LiveData as you do with RxJava. What best you can do to exploit the most of this duo is to use RxJava for your business logic (network call, data manipulation etc, anything that happens in and beyond Repository) and use LiveData for your presentation layer. By this, you get transformation and stream capabilities for your business logic and lifecycle-aware operation for your UI. LiveData and RxJava compliment each other if used together. What I mean is, do everything with RxJava and at the end when you want to update UI, do something like the code given below to change your Observable into LiveData. So, your View (UI) observes to the LiveData in ViewModel where your LiveData is nothing but non-mutable MutableLiveData (or MutableLiveData is mutable LiveData). So the question here is, why should you even use LiveData at the first place? As you can see below in the code, you store your response from RxJava to MutableLiveData (or LiveData) and your LiveData is lifecycle-aware, so in a way, your data is lifecycle-aware. Now, just imagine the possibility when your data itself know when and when-not-to update the UI. LiveData doesn't have a history (just the current state). Hence, you shouldn't use LiveData for a chat application. When you use LiveData with RxJava you don't need stuff like MediatorLiveData, SwitchMap etc. They are stream control tools and RxJava is better at that by many times. See LiveData as a data holder thing and nothing else. We can also say LiveData is lifecycle-aware consumer.
public class RegistrationViewModel extends ViewModel {
Disposable disposable;
private RegistrationRepo registrationRepo;
private MutableLiveData<RegistrationResponse> modelMutableLiveData =
new MutableLiveData<>();
public RegistrationViewModel() {
}
public RegistrationViewModel(RegistrationRepo registrationRepo) {
this.registrationRepo = registrationRepo;
}
public void init(RegistrationModel registrationModel) {
disposable = registrationRepo.loginForUser(registrationModel)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Response<RegistrationResponse>>() {
@Override
public void accept(Response<RegistrationResponse>
registrationModelResponse) throws Exception {
modelMutableLiveData.setValue(registrationModelResponse.body());
}
});
}
public LiveData<RegistrationResponse> getModelLiveData() {
return modelMutableLiveData;
}
@Override
protected void onCleared() {
super.onCleared();
disposable.dispose();
}
}
LiveData只是数据持有者而已。我们也可以说LiveData是具有生命周期意识的消费者。强烈建议LiveData了解生命周期的概念和相关对象LifeCycleOwner/ lifecycle,您可以获得业务逻辑的转换和流功能,以及UI的生命周期感知操作。
Rx是一个强大的工具,能够以优雅的声明式风格解决问题。它处理业务端选项或服务Api操作
推荐文章
- apk (.apk)和应用程序包(.aab)的区别
- 如何设置超时在改造库?
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- TextView的字体大小在Android应用程序改变字体大小从本机设置
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标
- Android Eclipse -无法找到*.apk
- 设置TextView文本从html格式的字符串资源在XML
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Android加载JS包失败
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的