forked from mcxiaoke/RxDocs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mcxiaoke
committed
Jul 24, 2015
1 parent
18984c6
commit b79a305
Showing
4 changed files
with
156 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
## Create | ||
|
||
使用一个函数从头开始创建一个Observable | ||
|
||
 | ||
|
||
你可以使用`Create `操作符从头开始创建一个Observable,给这个操作符传递一个接受观察者作为参数的函数,编写这个函数让它的行为表现为一个Observable--恰当的调用观察者的onNext,onError和onCompleted方法。 | ||
|
||
一个形式正确的有限Observable必须尝试调用观察者的onCompleted正好一次或者它的onError正好一次,而且此后不能再调用观察者的任何其它方法。 | ||
|
||
 | ||
|
||
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)) |
Oops, something went wrong.