Skip to content

Commit

Permalink
transalte how-to-use-rxjava
Browse files Browse the repository at this point in the history
  • Loading branch information
mcxiaoke committed Jul 24, 2015
1 parent 18984c6 commit b79a305
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 90 deletions.
27 changes: 9 additions & 18 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* [Single](Single.md)
* [Subject](Subject.md)
* [Scheduler](Scheduler.md)
* [所有的操作符列表](All-Operators-List.md)
* [操作符分类列表](Operators.md)
* [Operators](Operators.md)
* [创建操作](operators/Creating-Observables.md)
* [Create](operators/Create.md)
* [Defer](operators/Defer.md)
Expand Down Expand Up @@ -61,23 +60,14 @@
* [Using](operators/Using.md)
* [To](operators/To.md)
* [条件和布尔操作](operators/Conditional-and-Boolean-Operators.md)
* [All](operators/All.md)
* [Amb](operators/Amb.md)
* [Contains](operators/Contains.md)
* [DefaultIfEmpty](operators/DefaultIfEmpty.md)
* [SequenceEqual](operators/SequenceEqual.md)
* [SkipUntil](operators/SkipUntil.md)
* [SkipWhile](operators/SkipWhile.md)
* [TakeUntil](operators/TakeUntil.md)
* [TakeWhile](operators/TakeWhile.md)
* [All/Contains/Amb](operators/Conditional.md)
* [DefaultIfEmpty](operators/Conditional.md)
* [SequenceEqual](operators/Conditional.md)
* [SkipUntil/SkipWhile](operators/Conditional.md)
* [TakeUntil/TakeWhile](operators/Conditional.md)
* [算术和聚合操作](operators/Mathematical-and-Aggregate-Operators.md)
* [Average](operators/Average.md)
* [Concat](operators/Concat.md)
* [Count](operators/Count.md)
* [Max](operators/Max.md)
* [Min](operators/Min.md)
* [Reduce](operators/Reduce.md)
* [Sum](operators/Sum.md)
* [Average/Concat/Reduce](operators/Mathematical.md)
* [Max/Min/Count/Sum](operators/Mathematical.md)
* [异步操作](operators/Async-Operators.md)
* [连接操作](operators/Connectable-Observable-Operators.md)
* [Connect](operators/Connect.md)
Expand All @@ -86,6 +76,7 @@
* [Replay](operators/Replay.md)
* [阻塞操作](operators/Blocking-Observable-Operators.md)
* [字符串操作](operators/String-Observables.md)
* [按字母顺序的操作符列表](All-Operators-List.md)
* [RxJava文档和教程](Topics.md)
* [RxJava入门指南](topics/Getting-Started.md)
* [RxJava使用示例](topics/How-To-Use-RxJava.md)
Expand Down
18 changes: 9 additions & 9 deletions operators/Async-Operators.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
下面的这些操作符属于单独的`rxjava-async`模块,它们用于将同步方法转换为Observables。

* [**`start( )`**](Start.md) — 创建一个Observable,它发送一个函数的返回值
* [**`toAsync( )` or `asyncAction( )` or `asyncFunc( )`**](Start.md) — 将一个函数或者Action转换为已Observable,它执行这个函数并发送函数的返回值
* [**`startFuture( )`**](Start.md) — 将一个返回Future的函数转换为一个Observable,它发送Future的返回值
* [**`deferFuture( )`**](Start.md) — 将一个返回Observable的Future转换为一个Observable,但是并不尝试获取这个Future返回的Observable,直到有订阅者订阅它
* [**`forEachFuture( )`**](Start.md) — 传递Subscriber方法给一个Subscriber,但是同时表现得像一个Future一样阻塞直到它完成
* [**`fromAction( )`**](Start.md) — 将一个Action转换为Observable,当一个订阅者订阅时,它执行这个action并发送它的返回值
* [**`fromCallable( )`**](Start.md) — 将一个Callable转换为Observable,当一个订阅者订阅时,它执行这个Callable并发送Callable的返回值,或者发送异常
* [**`fromRunnable( )`**](Start.md) — convert a Runnable into an Observable that invokes the runable and emits its result when a Subscriber subscribes将一个Runnable转换为Observable,当一个订阅者订阅时,它执行这个Runnable发送Runnable的返回值
* [**`runAsync( )`**](Start.md) — 返回一个StoppableObservable,它发送某个Scheduler上指定的Action生成的多个actions
* [**`start( )`**](Start.md#start) — 创建一个Observable,它发送一个函数的返回值
* [**`toAsync( )` or `asyncAction( )` or `asyncFunc( )`**](Start.md#toAsync) — 将一个函数或者Action转换为已Observable,它执行这个函数并发送函数的返回值
* [**`startFuture( )`**](Start.md#startFuture) — 将一个返回Future的函数转换为一个Observable,它发送Future的返回值
* [**`deferFuture( )`**](Start.md#deferFuture) — 将一个返回Observable的Future转换为一个Observable,但是并不尝试获取这个Future返回的Observable,直到有订阅者订阅它
* [**`forEachFuture( )`**](Start.md#forEachFuture) — 传递Subscriber方法给一个Subscriber,但是同时表现得像一个Future一样阻塞直到它完成
* [**`fromAction( )`**](Start.md#fromAction) — 将一个Action转换为Observable,当一个订阅者订阅时,它执行这个action并发送它的返回值
* [**`fromCallable( )`**](Start.md#fromCallable) — 将一个Callable转换为Observable,当一个订阅者订阅时,它执行这个Callable并发送Callable的返回值,或者发送异常
* [**`fromRunnable( )`**](Start.md#fromRunnable) — convert a Runnable into an Observable that invokes the runable and emits its result when a Subscriber subscribes将一个Runnable转换为Observable,当一个订阅者订阅时,它执行这个Runnable发送Runnable的返回值
* [**`runAsync( )`**](Start.md#runAsync) — 返回一个StoppableObservable,它发送某个Scheduler上指定的Action生成的多个actions
66 changes: 66 additions & 0 deletions operators/Creating.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
## Create

使用一个函数从头开始创建一个Observable

![create](../images/operators/create.c.png)

你可以使用`Create `操作符从头开始创建一个Observable,给这个操作符传递一个接受观察者作为参数的函数,编写这个函数让它的行为表现为一个Observable--恰当的调用观察者的onNext,onError和onCompleted方法。

一个形式正确的有限Observable必须尝试调用观察者的onCompleted正好一次或者它的onError正好一次,而且此后不能再调用观察者的任何其它方法。

![create](../images/operators/create.png)

RxJava将这个操作符实现为 `create` 方法。

建议你在传递给`create`方法的函数中检查观察者的`isUnsubscribed`状态,以便在没有观察者的时候,让你的Observable停止发送数据或者做昂贵的运算。

示例代码:

```java

Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> observer) {
try {
if (!observer.isUnsubscribed()) {
for (int i = 1; i < 5; i++) {
observer.onNext(i);
}
observer.onCompleted();
}
} catch (Exception e) {
observer.onError(e);
}
}
} ).subscribe(new Subscriber<Integer>() {
@Override
public void onNext(Integer item) {
System.out.println("Next: " + item);
}

@Override
public void onError(Throwable error) {
System.err.println("Error: " + error.getMessage());
}

@Override
public void onCompleted() {
System.out.println("Sequence complete.");
}
});

```

输出:

```
Next: 1
Next: 2
Next: 3
Next: 4
Sequence complete.
```

`create`方法默认不在任何特定的调度器上执行。

* Javadoc: [`create(OnSubscribe)`](http://reactivex.io/RxJava/javadoc/rx/Observable.html#create(rx.Observable.OnSubscribe))
Loading

0 comments on commit b79a305

Please sign in to comment.