码迷,mamicode.com
首页 > 移动开发 > 详细

rxjava-android 官方介绍

时间:2016-11-19 02:08:50      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:lin   ref   canonical   example   als   groovy   tco   apple   import   

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-android 官方介绍

标签:lin   ref   canonical   example   als   groovy   tco   apple   import   

原文地址:http://www.cnblogs.com/baiqiantao/p/61461845a63c472074d99f8972757323.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!