RxJava RxAndroid【简介】
资源
RxAndroid :https://github.com/ReactiveX/RxAndroidRxJava的 wiki:https://github.com/ReactiveX/rxjava/wikiRxJava中文文档:https://mcxiaoke.gitbooks.io/rxdocs/content/ (部分有点旧)引入依赖: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 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 givenLooper.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二进制文件
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 buildFurther 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声明 yourObservableto 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
Observableon a new thread, and emit发射 results throughonNexton 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 anObservableon an arbitraryLooper, create an associated合作的Schedulerby callingAndroidSchedulers.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
onNexton whatever thread is runningbackgroundLooper.
RxJava RxAndroid【简介】的更多相关文章
- Android RxJava/RxAndroid结合Retrofit使用
概述 RxJava是一个在 Java VM 上使用可观測的序列来组成异步的.基于事件的程序的库.更重要的是:使用RxJava在代码逻辑上会非常简洁明了,尤其是在复杂的逻辑上.告别迷之缩进. RxAnd ...
- RxJava / RxAndroid
RxJava 是什么 RxJava 是函数响应式编程框架,它用观察者设计模式. 常用来做异步数据处理,在安卓中用来代替传统的 AsyncTask + Handler 的组合结构. RxJava 架构简 ...
- RxJava Rxandroid retrofit
其实Retrofit会了.集合RxJava,RxAndroid 就很简单了. 只需要改几个地方. 1.接口里面返回的对象不再是 call,而是Observable public interface A ...
- Android 基于ijkplayer+Rxjava+Rxandroid+Retrofit2.0+MVP+Material Design的android万能播放器aaa
MDPlayer万能播放器 MDPlayer,基于ijkplayer+Rxjava+Rxandroid+Retrofit2.0+MVP+Material Design的android万能播放器,可以播 ...
- Rxjava, RxAndroid, Retrofit 等库的使用
RxJava的基本用法: 关于 unSubscribe() 的调用问题: There is no need to unsubscribe in onCompleted. Take a look at ...
- RxJava/RxAndroid 使用实例实践
原文地址 RxAndroid Tutorial响应式编程(Reactive programming)不是一种API,而是一种新的非常有用的范式,而RxJava就是一套基于此思想的框架,在Android ...
- 综合开源框架之RxJava/RxAndroid
* 一种帮助做异步的框架. 类似于 AsyncTask. 但其灵活性和扩展性远远强于前者. * 主页: https://github.com/ReactiveX/RxJava * 中文资料: * ht ...
- rxjava rxandroid使用遇到的坑
今天在解决一个界面加载本地数据库数据的时候,使用rxjava在指定io线程操作是遇到一个问题,即使指定了在io线程操作,可是界面还是卡顿,最后通过打印线程Thread.currentThread(). ...
- RxJava+RxAndroid+MVP入坑实践(基础篇)
转载请注明出处:http://www.blog.csdn.net/zhyxuexijava/article/details/51597230.com 前段时间看了MVP架构和RxJava,最近也在重构 ...
随机推荐
- Android 中文 API (29) —— CompoundButton
前言 本章内容是android.widget.CompoundButton,翻译来自德罗德,再次感谢德罗德 !期待你一起参与Android API 的中文翻译,联系我over140@gmail.com ...
- centos 下vmware 下添加硬盘到root
### #vmware 里找到硬盘拖大点...,如果不想从启动么,添加个新 #的也行.不过那个是另外的方法了 ### #### ##找下硬盘添加在哪里 #### fdisk -l //创建分 ...
- 在zend studio 9.* 中使用phpunit进行单元测试
单元测试在用PHP开发大型项目时必备的减少测试难度和提高测试效率的利器,而PHPUnit是php做单元测试时使用范围最广的一个.如果在window系统中开发,就要调用控制台来运行phpunit,非常的 ...
- Unity3d 项目管理
Unity3d 项目管理 1.项目管理采用TortoiseSVN方式进行管理,但是也要结合人员管理的方式,尽量在U3D中多采用Scene(关卡的方式),以一下目录的方式进行管理! 以名字的方式进行合 ...
- Qt如何去掉按钮等控件的虚线框(焦点框)(三种办法)
方法1:可以通过代码ui->pushButton->setFocusPolicy(Qt::NoFocus)或在Qt Creator的属性列表中设置. 方法2:如果在嵌入式设备中需要通过按键 ...
- UIAutomation识别UI元素
MS UI Automation(Microsoft User Interface Automation:UIA)是随.net framework3.0一起发布的,虽然在如今这个几乎每天都有各种新名词 ...
- Netty4.0学习笔记系列之一:Server与Client的通讯
http://blog.csdn.net/u013252773/article/details/21046697 本文是学习Netty的第一篇文章,主要对Netty的Server和Client间的通讯 ...
- 【HDOJ】2319 Card Trick
水题,STL双端队列. /* 2319 */ #include <iostream> #include <cstdio> #include <cstring> #i ...
- 【单调栈】Vijos P1926 紫色的手链
题目链接: https://vijos.org/p/1926 题目大意: 给n个数(n<=100 000),求任意区间的最大值异或次大值的最大值. 题目思路: [模拟][单调栈] 我们维护一个严 ...
- poj3186 Treats for the Cows
http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total S ...