我没有从Android架构组件中获得在Android和LiveData中使用RxJava的理由。如果以代码的形式解释用例和两者之间的差异,以及解释两者之间差异的示例示例,将会非常有帮助。


当前回答

LiveData部分等于Rx Subject或SharedRxObservable LiveData管理订阅的生命周期,但Rx主题 订阅应该手动创建和处理 LiveData没有终止状态,但Rx Subject有OnError 和oncomplete

其他回答

正如你所知,在响应式生态系统中,我们有一个发出数据的Observable和一个订阅(获得通知)这个Observable发出的Observer,所谓的Observer模式是如何工作的并没有什么奇怪的。一个可观察对象“喊”了什么,观察者会得到通知,可观察对象在给定时刻喊了什么。

把LiveData看作一个可观察对象,它允许你管理处于活动状态的观察者。换句话说,LiveData是一个简单的可观察对象,但也负责生命周期。

但是让我们看看你请求的两个代码案例:

A)实时数据

(B) RXJava

A)这是LiveData的一个基本实现

1)你通常在ViewModel中实例化LiveData来维护方向变化(你可以有只读的LiveData,或可写的mutabllivedata,所以你通常从类LiveData外部公开)

2)在Main Activity的OnCreate方法中(不是ViewModel) 你“订阅”一个观察者对象(通常是一个onChanged方法)

3)你启动方法观察建立链接

首先是ViewModel(拥有业务逻辑)

class ViewModel : ViewModel() { //Point 1

    var liveData: MutableLiveData<Int> = MutableLiveData()

}

这是MainActivity(尽量哑)

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val ViewModelProvider= ViewModelProviders.of(this).get(ViewModel::class.java)

        ViewModelProvider.observe(this, Observer {//Points 2 and 3
            //what you want to observe
        })


        }
    }
}

B)这是RXJava的基本实现

1)你声明一个可观察对象

2)您声明为观察员

3)你订阅观察者的可观察对象

Observable.just(1, 2, 3, 4, 5, 6) // Point 1

   .subscribe(new Subscriber() {    //Points 2 & 3
       @Override
       public void onCompleted() {
           System.out.println("Complete!");
       }

       @Override
       public void onError(Throwable e) {
       }

       @Override
       public void onNext(Double value) {
           System.out.println("onNext: " + value);
       }
    });

In particular LiveData is used with Lifecycle and often with ViewModel(as we have seen) architecture components. In fact when LiveData is combined with a ViewModel allows you to keep updated in real time every change in the Observer, so that the events are managed in real time where is needed. To use LiveData is strongly recommended to know the concept of lifecycle and the relative objects LifeCycleOwner/LifeCycle, also I would suggest you to have a look at Transformations, if you want to implement LiveData in real life scenarios. Here you can find some use cases from the great commonsware.

To wrap up basically LiveData is a simplified RXJava, an elegant way to observe changes across multiple components without creating explicit so called dependency rules between the components, so that you can test much easier the code and make it much more readable. RXJava, allows you to do the things of LiveData and much more. Because of the extended functionalities of RXJava, you can both use LiveData for simple cases or exploit all the power of RXJava keep using Android Architecture components as the ViewModel, of course this means that RXJava can be far more complex, just think has hundreds of operators instead of SwitchMap and Map of LiveData(at the moment).

RXJava版本2是一个革命性的面向对象范式的库,增加了一种所谓的函数式方式来管理程序流。

LiveData部分等于Rx Subject或SharedRxObservable LiveData管理订阅的生命周期,但Rx主题 订阅应该手动创建和处理 LiveData没有终止状态,但Rx Subject有OnError 和oncomplete

比较LiveData和RxJava就像比较苹果和水果沙拉。

比较LiveData和contenttobserver,你是在比较苹果和苹果。LiveData有效地替代了contenttobserver的生命周期。

将RxJava与AsyncTask或任何其他线程工具进行比较,就像将水果沙拉与橙子进行比较一样,因为RxJava帮助的不仅仅是线程。

LiveData是android架构组件的一个子集,由android团队开发。

对于活动数据和其他体系结构组件,内存泄漏和其他类似问题由体系结构组件处理。由于它是由android团队开发的,所以它是android最好的。他们还提供处理新版本Android的更新。

如果你只想在Android应用程序开发中使用,请使用Android架构组件。否则,如果你想使用其他Java应用程序,如web应用程序,桌面应用程序等,请使用RxJava

我的简单回答是不要使用RxJava。这是一种过于复杂和滥用的方式。对于所有使用RxJava的项目来说,它们都很难维护和调试。RxJava是异步的,使用线程来分派请求,一般来说这是完全不必要的,如果需要的话,Kotlin协程在99%的情况下做得更好。