RxJava RxAndroid【简介】


资源
 
       
 
       
 
         
        
    compile 'io.reactivex:rxandroid:1.2.1'
    // 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.
    compile 'io.reactivex:rxjava:1.1.6'

RxJava简介
 
      
 
      
 
       
RxJava 在 GitHub 主页上的自我介绍是
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上的一个实现:一个使用可观测的序列(observable sequences)来组成(composing )异步的(asynchronous )、基于事件(event-based)的程序的库。

这里面有几个关键词:
  • 组合(Composing):Reactive Extension的首要目标之一就是将多种异步操作组合起来使得代码更加简单。要做到这一点,数据流必须定义清楚,这样代码就很清晰集中,使得异步操作代码异步处理代码不会充斥整个应用程序。
  • 异步(Asynchronous):虽然Rx不仅仅能处理异步操作,但是使用Rx,大大简化了异步操作的实现,并且代码容易理解进而容易维护。
  • 基于事件(Event-based):Rx简化了传统的异步编程方式
  • 可观察序列(Observable sequences):Obervable sequences是Rx的核心,它是一种集合,集合的元素在第一次访问的时候肯能还没有填充。
其实, RxJava 的本质可以压缩为异步这一个词。说到根上,它就是一个实现异步操作的库,而别的定语都是基于这之上的。


RxJava 好在哪
换句话说,『同样是做异步,为什么人们用它,而不用现成的 AsyncTask / Handler / XXX / ... ?』
一个词:简洁。
异步操作很关键的一点是程序的简洁性,因为在调度过程比较复杂的情况下,异步代码经常会既难写也难被读懂。 Android 创造的 AsyncTask 和Handler ,其实都是为了让异步代码更加简洁。RxJava 的优势也是简洁,但它的简洁的与众不同之处在于,随着程序逻辑变得越来越复杂,它依然能够保持简洁。


RxJava是目前在Android开发者中新兴热门的函数库。唯一的问题是 刚开始接触时会感到较难理解 函数响应式编程 对于“外面世界”来的开发人员而言是很难理解的,但一旦理解了它,你会感觉真是太棒了。

Rx是对LINQ(语言集成查询 Language Integrated Query)的一种扩展, 他的目标是对异步的集合进行操作,也就是说,集合中的元素是异步填充的,比如说从Web或者云端获取数据然后对集合进行填充。Rx起源于 Microsoft DevLabs小组的研究,他扩展了LINQ的一些特性,目前Rx支持多种平台如 JavaScript,Windows Phone,ios,Android 。随着数据处理变得复杂,LINQ使得我们的处理逻辑变得简单清晰,同样地,随着越来越多的数据通过从云端异步获取,Rx使得这种异步数据处理操作变得简单和容易维护。
 
在处理静态集合数据方面,LINQ使用类似SQL的语法来操作和使用不同来源的数据。相反, Rx被设计出来用来处理将来才会填充好的集合,也就是说,集合类型定义好了,但是集合中的元素可能在未来的某一时刻才会被填充。

LINQ和Rx在技术上有很多相似的地方。在LINQ对集合进行一系列操作如添加,移除,修改,提取后, 会得到一个新的集合,新集合只是原始集合的一个修改版本。Rx也是一样,集合和数据流看起来非常不同,但是他们在很多关键的地方有联系,这就是我们将数据流称之为未来的集合的原因。集合和数据流都是多数据按某种顺序进行排列。LINQ和Rx可以这些序列进行一系列操作然后得到一个新的序列。

Rx提供了一种新的组织和协调异步事件的方式,例如协调多个从云端返回的多个异步的数据流。Rx能够使得我们用一个简单的方式来处理这些数据流,极大的简化了代码的编写。例如,.NET中传统的Begin/End异步编程模式在处理单个异步操作时可以应付,但是如果同时多个异步调用时,线程控制就会使得代码变得比较复杂。使用Rx,Begin/End模式就变成了一条简单的方法,这使得代码更加清晰和容易理解。

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 GroovyClojureJRubyKotlinand 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:


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:

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.
  • RxAndroid: 687474703a2f2f696d672e736869656c64732e696f2f6d6176656e2d63656e7472616c2f762f696f2e7265616374697665782f7278616e64726f69642e737667
  • RxJava: 687474703a2f2f696d672e736869656c64732e696f2f6d6176656e2d63656e7472616c2f762f696f2e7265616374697665782f72786a6176612e737667

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.

68747470733a2f2f6170692e7472617669732d63692e6f72672f5265616374697665582f5278416e64726f69642e7376673f6272616e63683d312e78

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合作的 Schedulerby 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.






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值