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,最近也在重构 ...
随机推荐
- python中归并排序
# coding=UTF-8 #!/usr/bin/python import sys def merge(nums, first, middle, last): "merge" ...
- WPF学习笔记-自定义窗口
代码部分 <Style x:Key="for_noresize_window" TargetType="{x:Type Window}"> < ...
- 使用单调队列优化的 O(nm) 多重背包算法
我搜索了一下,找到了一篇很好的博客,讲的挺详细:链接. 解析 多重背包的最原始的状态转移方程: 令 c[i] = min(num[i], j / v[i]) f[i][j] = max(f[i-1][ ...
- js/bat批处理调用谷歌浏览器chrome批量打开网页测试web性能
批处理批量打开网页 其实用java就可以搞定,但是这么一个轻巧的测试,js或者bat批处理去一次性打开几百个网页测试一下页面没必要上java 两者的区别,js的话,只能打开多个浏览器实例,不方便查看效 ...
- JS----Issue
HTTP Post Form Data:以明文提交的,因此要加密 http://pajhome.org.uk/crypt/md5/index.html
- struts配置时遇到的几个问题
1. struts在配置文件的时候,如果package包继承为 :extends="json-default" ,那么项目中要引入struts2-json-plugin-xxx.j ...
- win平台下, 检测网络是否连接最好的办法
[Delphi]检查URL是否有效的函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 function CheckUr ...
- 在InnoDB,记录在 non-clustered indexes(也被称为secondary indexes) 包含了主键值
In InnoDB, the records in non-clustered indexes (also called secondary indexes) contain the primary ...
- 【转】google chrome如何设置主页
原文网址:http://jingyan.baidu.com/article/8275fc86bf916c46a13cf666.html google chrome是一款拥有众多优秀插件的浏览器,是我们 ...
- 【模拟】NEERC15 J Jump(2015-2016 ACM-ICPC)(Codeforces GYM 100851)
题目链接: http://codeforces.com/gym/100851 题目大意: 系统里生成一个字符串C,一开始告诉你字符串的长度N(偶数).接着你需要在n+500次内猜出这个字符串是什么. ...