资源
RxJava简介
- 组合(Composing):Reactive Extension的首要目标之一就是将多种异步操作组合起来使得代码更加简单。要做到这一点,数据流必须定义清楚,这样代码就很清晰集中,使得异步操作代码异步处理代码不会充斥整个应用程序。
- 异步(Asynchronous):虽然Rx不仅仅能处理异步操作,但是使用Rx,大大简化了异步操作的实现,并且代码容易理解进而容易维护。
- 基于事件(Event-based):Rx简化了传统的异步编程方式
- 可观察序列(Observable sequences):Obervable sequences是Rx的核心,它是一种集合,集合的元素在第一次访问的时候肯能还没有填充。
RxJava官方介绍
RxJava is a Java VM implementation of ReactiveX (Reactive Extensions): a library for composing asynchronous and event-based programs by using observable sequences.
RxJava是 ReactiveX 在JVM上的一个实现:一个使用可观测的序列来组成异步的、基于事件的程序的库。
For more information about ReactiveX, see the Introduction to ReactiveX page.
RxJava is Lightweight轻量级的
RxJava tries to be very lightweight. It is implemented as a single JAR that is focused on just the Observable abstraction and related higher-order functions仅关注Observable的抽象和与之相关的高层函数. You could implement a composable Future that is similarly unbiased, but Akka Futures for example come tied in with an Actor library and a lot of other stuff.)
RxJava is a Polyglot Implementation多语言实现
RxJava supports Java 6 or higher and JVM-based languages such as Groovy, Clojure, JRuby, Kotlinand Scala.
RxJava is meant for a more polyglot environment than just Java/Scala, and it is being designed to respect尊重 the idioms习惯 of each JVM-based language. (This is something we’re still working on.)
RxJava Libraries
The following external第三方 libraries can work with RxJava:
- Hystrix latency and fault tolerance bulkheading library.
- Camel RX provides an easy way to reuse any of the Apache Camel components, protocols, transports and data formats with the RxJava API
- rxjava-http-tail allows you to follow logs over HTTP, like
tail -f
- mod-rxvertx - Extension for VertX that provides support for Reactive Extensions (RX) using the RxJava library
- rxjava-jdbc - use RxJava with jdbc connections to stream ResultSets and do functional composition of statements
- rtree - immutable in-memory R-tree and R*-tree with RxJava api including backpressure
RxAndroid官方介绍
RxAndroid: Reactive Extensions for Android
Android specific bindings for RxJava.
This module adds the minimum最小量的 classes to RxJava that make writing reactive components组件 in Android applications easy and hassle-free省事. More specifically, it provides a Scheduler
调度程序 that schedules on the main thread or any given Looper
.
Communication
Since RxAndroid is part of the RxJava family the communication channels are similar:
- Google Group: RxJava
- Twitter: @RxJava
- StackOverflow: rx-android
- GitHub Issues
Binaries二进制文件
// Because RxAndroid releases are few and far between, it is recommended you also// explicitly depend on RxJava's latest version for bug fixes and new features.
Additional补充 binaries and dependency information for can be found at http://search.maven.org.
Build
To build:
$ git clone git@github.com:ReactiveX/RxAndroid.git
$ cd RxAndroid/
$ ./gradlew build
Further details on building can be found on the RxJava Getting Started page of the wiki.
Sample usage
A sample project which provides runnable code examples that demonstrate演示 uses of the classes in this project is available in the sample-app/
folder.
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:
Observable."one""two""three""four""five"/* an Observer */
This will execute执行 the Observable
on a new thread, and emit发射 results through onNext
on the main thread.
Observing on arbitrary任意 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
:
Looper=// ...Observable."one""two""three""four""five"/* an Observer */
This will execute the Observable on a new thread and emit results through onNext
on whatever thread is runningbackgroundLooper
.