资源


    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二进制文件

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'
  • RxAndroid: 
  • RxJava: 

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.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.

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 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 runningbackgroundLooper.


RxJava RxAndroid【简介】的更多相关文章

  1. Android RxJava/RxAndroid结合Retrofit使用

    概述 RxJava是一个在 Java VM 上使用可观測的序列来组成异步的.基于事件的程序的库.更重要的是:使用RxJava在代码逻辑上会非常简洁明了,尤其是在复杂的逻辑上.告别迷之缩进. RxAnd ...

  2. RxJava / RxAndroid

    RxJava 是什么 RxJava 是函数响应式编程框架,它用观察者设计模式. 常用来做异步数据处理,在安卓中用来代替传统的 AsyncTask + Handler 的组合结构. RxJava 架构简 ...

  3. RxJava Rxandroid retrofit

    其实Retrofit会了.集合RxJava,RxAndroid 就很简单了. 只需要改几个地方. 1.接口里面返回的对象不再是 call,而是Observable public interface A ...

  4. Android 基于ijkplayer+Rxjava+Rxandroid+Retrofit2.0+MVP+Material Design的android万能播放器aaa

    MDPlayer万能播放器 MDPlayer,基于ijkplayer+Rxjava+Rxandroid+Retrofit2.0+MVP+Material Design的android万能播放器,可以播 ...

  5. Rxjava, RxAndroid, Retrofit 等库的使用

    RxJava的基本用法: 关于 unSubscribe() 的调用问题: There is no need to unsubscribe in onCompleted. Take a look at  ...

  6. RxJava/RxAndroid 使用实例实践

    原文地址 RxAndroid Tutorial响应式编程(Reactive programming)不是一种API,而是一种新的非常有用的范式,而RxJava就是一套基于此思想的框架,在Android ...

  7. 综合开源框架之RxJava/RxAndroid

    * 一种帮助做异步的框架. 类似于 AsyncTask. 但其灵活性和扩展性远远强于前者. * 主页: https://github.com/ReactiveX/RxJava * 中文资料: * ht ...

  8. rxjava rxandroid使用遇到的坑

    今天在解决一个界面加载本地数据库数据的时候,使用rxjava在指定io线程操作是遇到一个问题,即使指定了在io线程操作,可是界面还是卡顿,最后通过打印线程Thread.currentThread(). ...

  9. RxJava+RxAndroid+MVP入坑实践(基础篇)

    转载请注明出处:http://www.blog.csdn.net/zhyxuexijava/article/details/51597230.com 前段时间看了MVP架构和RxJava,最近也在重构 ...

随机推荐

  1. Ubuntu14 或是其他系统当中关于sublimeSFTP超时解决方法

    一直都使用Sublime的SFTP功能,感觉还不错,好用,但是最近不知道怎么了,使用不成了,提示超时了,于是在网上找一下解决方法,没有找到,于是自己想着试试看,于在搞了一会,发现只要把配制文件当中的s ...

  2. TatukGIS-TGIS_Editor.CreateShape

    procedure CreateShape(const _layer: TObject; const _ptg: TGIS_Point3D; const _type: TGIS_ShapeType; ...

  3. Python,遍历目录下TXT

    import os #获取根目录,递归得到所以txt文件的路径 list_dirs = os.walk(os.curdir) txtfilenames=[] for root, dirs, files ...

  4. BAE 环境下配置 struts2 + spring + hibernate(SSH)(三)spring&hibernate

    1.在lib中加入必要的包,导入后结果如下: lib打包下载:SSH-lib.jar  (struts2.3.1.2  spring3.0.5 hibernate3.6.10.Final) 只包含必要 ...

  5. 尽量使用ToUpper比较,避免使用ToLower

    在编码时尽量使用ToUpper比较,避免使用ToLower,因为微软对ToUpper进行了优化,以下为测试结果: public void TestToLower() { Stopwatch watch ...

  6. Java Web开发介绍

    转自:http://www.cnblogs.com/pythontesting/p/4963021.html Java Web开发介绍 简介 Java很好地支持web开发,在桌面上Eclipse RC ...

  7. MAC——laravel环境

    apache: 目录:etc/apache2 重启:sudo apachectl restart PHP: 把/etc/php.ini.default复制并重命名/ect/php.ini 打开Load ...

  8. nosql db and javascript performance

    http://blog.csdn.net/yiqijinbu/article/details/9053467 http://blog.nosqlfan.com/tags/javascript http ...

  9. SHELL要发送HTML这类邮件的话,还得靠msmtp 和 mutt

    参考蛮多的.. http://storysky.blog.51cto.com/628458/293005 http://www.wilf.cn/post/centos-mutt-msmtp-setup ...

  10. CreateProcessWithLogonW(好像可以指定进程的上下文环境)

    Creates a new process and its primary thread. Then the new process runs the specified executable fil ...