RxAndroid解析

转自:http://ranseti.top/article/rxandroid

RxAndroid是开源项目集合ReactiveX中用于平台和框架的一个开源库,GitHub地址https://github.com/ReactiveX/RxAndroid。使用RxAndroid需要依赖于Rxjava。



该模块将最小类添加到RxJava中,使得在Android应用程序中编写反应式组件变得简单而轻松。 更具体地说,它提供了一个调度程序在主线程或任何给定的Looper上进行调度。

Observing on the main thread(在主线上观察)

One of the most common operations when dealing with asynchronous tasks on Android is to observe the task's result or outcome on the main thread. Using vanilla Android, this would typically be accomplished with an AsyncTask. With RxJava instead you would declare your Observable to be observed on the main thread:
(在Android上处理异步任务时最常见的操作之一是在主线程上观察任务的结果或结果。 使用vanilla Android,通常使用AsyncTask来完成。 用RxJava代替你会在主线程中声明你的Observable:

Observable.just("one", "two", "three", "four", "five")
        .subscribeOn(Schedulers.newThread())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(/* an Observer */);

This will execute the Observable on a new thread, and emit results through onNext on the main thread.
(这将在新线程上执行Observable,并通过主线程上的onNext发送结果。)

Observing on arbitrary loopers(在任意的loopers上观察)

The previous sample is merely a specialization of a more general concept: binding asynchronous communication to an Android message loop, or Looper. In order to observe an Observable on an arbitrary Looper, create an associated Scheduler by calling AndroidSchedulers.from:
(前面的示例仅仅是一个更一般概念的专门化:将异步通信绑定到Android消息循环或Looper。 为了在一个任意的Looper上观察一个Observable,通过调用AndroidSchedulers.from创建一个关联的Scheduler:)

Looper backgroundLooper = // ...
Observable.just("one", "two", "three", "four", "five")
        .observeOn(AndroidSchedulers.from(backgroundLooper))
        .subscribe(/* an Observer */)

This will execute the Observable on a new thread and emit results through onNext on whatever thread is running backgroundLooper.
(这将在一个新的线程上执行Observable,并在任何运行backgroundLooper的线程上通过onNext发送结果。)



看完RxAndroid官方介绍后对RxAndroid和RxJava的认识还是比较模糊,不过可以看看朱凯大神的解析http://gank.io/post/560e15be2dca930e00da1083。

你可能感兴趣的:(RxAndroid解析)