我没有从Android架构组件中获得在Android和LiveData中使用RxJava的理由。如果以代码的形式解释用例和两者之间的差异,以及解释两者之间差异的示例示例,将会非常有帮助。
当前回答
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是android架构组件的一个子集,由android团队开发。
对于活动数据和其他体系结构组件,内存泄漏和其他类似问题由体系结构组件处理。由于它是由android团队开发的,所以它是android最好的。他们还提供处理新版本Android的更新。
如果你只想在Android应用程序开发中使用,请使用Android架构组件。否则,如果你想使用其他Java应用程序,如web应用程序,桌面应用程序等,请使用RxJava
我的简单回答是不要使用RxJava。这是一种过于复杂和滥用的方式。对于所有使用RxJava的项目来说,它们都很难维护和调试。RxJava是异步的,使用线程来分派请求,一般来说这是完全不必要的,如果需要的话,Kotlin协程在99%的情况下做得更好。
关于最初的问题,RxJava和LiveData都能很好地互补。
LiveData在ViewModel层上表现出色,它与Android生命周期和ViewModel紧密集成。RxJava在转换方面提供了更多的功能(正如@Bob Dalgleish所提到的)。
目前,我们在数据源和存储库层使用RxJava,并在ViewModels中转换为LiveData(使用LiveDataReactiveStreams)(在将数据暴露给活动/片段之前)-非常满意这种方法。
推荐文章
- 如何以编程方式将视图添加到视图
- 单击url会打开默认浏览器
- 使用Retrofit刷新OAuth令牌,而不修改所有调用
- 多个dex文件定义了landoid /support/v4/accessibilityservice/AccessibilityServiceInfoCompat
- 如何获得动作栏的高度?
- 从活动外部调用startActivity() ?
- createScaledBitmap的过滤器参数做什么?
- 为什么我在使用adb时访问数据文件夹被拒绝?
- 如何确定Android设备的GPS是否已启用
- 没有找到与给定名称匹配的资源:attr 'android:keyboardNavigationCluster'。更新到支持库26.0.0时
- 禁用在android中打开导航抽屉的滑动手势
- Android:允许平板电脑竖屏和横屏,但强制手机竖屏?
- 如何使布局与视图填充剩余的空间?
- 字符串资源新行/n不可能?
- 如何点击或点击一个TextView文本