Skip to content

Commit df4121b

Browse files
Merge pull request #322 from mttkay/master
Update Android README.md
2 parents 208fe02 + 78a877a commit df4121b

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

rxjava-contrib/rxjava-android/README.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
Android specific bindings for Rx.
44

5+
This module adds a number of classes to RxJava that make writing reactive components in
6+
Android applications easy and hassle free. More specifically, it
7+
8+
- provides a `Scheduler` that schedules an `Observable` on a given Android `Handler` thread, particularly the main UI thread
9+
- provides base `Observer` implementations that make guarantees w.r.t. to reliable and thread-safe use throughout
10+
`Fragment` and `Activity` life-cycle callbacks (coming soon)
11+
- provides reusable, self-contained reactive components for common Android use cases and UI concerns (coming soon)
12+
13+
514
# Binaries
615

716
Binaries and dependency information for Maven, Ivy, Gradle and others can be found at [http://search.maven.org](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22hystrix-servo-metrics-publisher%22).
@@ -12,12 +21,60 @@ Example for [Maven](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22rxjava-and
1221
<dependency>
1322
<groupId>com.netflix.rxjava</groupId>
1423
<artifactId>rxjava-android</artifactId>
15-
<version>x.y.z</version>
24+
<version>0.10.1</version>
1625
</dependency>
1726
```
1827

1928
and for Ivy:
2029

2130
```xml
22-
<dependency org="com.netflix.rxjava" name="rxjava-android" rev="x.y.z" />
31+
<dependency org="com.netflix.rxjava" name="rxjava-android" rev="0.10.1" />
2332
```
33+
34+
# Sample usage
35+
36+
We are working on a samples project which provides runnable code samples that demonstrate common Rx patterns and
37+
their use in Android applications.
38+
39+
## Observing on the UI thread
40+
41+
One of the most common operations when dealing with asynchronous tasks on Android is to observe the task's
42+
result or outcome on the main UI thread. Using vanilla Android, this would
43+
typically be accomplished with an `AsyncTask`. With RxJava instead you would declare your `Observable`
44+
to be observed on the main thread:
45+
46+
public class ReactiveFragment extends Fragment {
47+
48+
@Override
49+
public void onCreate(Bundle savedInstanceState) {
50+
super.onCreate(savedInstanceState);
51+
Observable.from("one", "two", "three", "four", "five")
52+
.subscribeOn(Schedulers.newThread())
53+
.observeOn(AndroidSchedulers.mainThread())
54+
.subscribe(/* an Observer */);
55+
}
56+
57+
This will execute the Observable on a new thread, and emit results through `onNext` on the main UI thread.
58+
59+
## Observing on arbitrary threads
60+
The previous sample is merely a specialization of a more general concept, namely binding asynchronous
61+
communication to an Android message loop using the `Handler` class. In order to observe an `Observable`
62+
on an arbitrary thread, create a `Handler` bound to that thread and use the `AndroidSchedulers.handlerThread`
63+
scheduler:
64+
65+
new Thread(new Runnable() {
66+
@Override
67+
public void run() {
68+
final Handler handler = new Handler(); // bound to this thread
69+
Observable.from("one", "two", "three", "four", "five")
70+
.subscribeOn(Schedulers.newThread())
71+
.observeOn(AndroidSchedulers.handlerThread(handler))
72+
.subscribe(/* an Observer */)
73+
74+
// perform work, ...
75+
}
76+
}, "custom-thread-1").start();
77+
78+
This will execute the Observable on a new thread and emit results through `onNext` on "custom-thread-1".
79+
(This example is contrived since you could as well call `observeOn(Schedulers.currentThread())` but it
80+
shall suffice to illustrate the idea.)

0 commit comments

Comments
 (0)