Create, Defer, Empty/Never/Throw, From, Interval, Just, Range, Repeat, Start, and Timer
Observable.create(new Observable.OnSubscribe() {
@Override
public void call (Subscriber 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() {
@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.
Integer[] items = { 0, 1, 2, 3, 4, 5 };
Observable myObservable = Observable.from(items);
myObservable.subscribe(
new Action1() {
@Override
public void call(Integer item) {
System.out.println(item);
}
},
new Action1() {
@Override
public void call(Throwable error) {
System.out.println("Error encountered: " + error.getMessage());
}
},
new Action0() {
@Override
public void call() {
System.out.println("Sequence complete");
}
}
);
运行结果如下:
0
1
2
3
4
5
Sequence complete
Observable.just(1, 2, 3)
.subscribe(new Subscriber() {
@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
Sequence complete.
// 下面通过比较defer操作符和just操作符的运行结果作比较:
i=10;
Observable justObservable = Observable.just(i);
i=12;
Observable deferObservable = Observable.defer(new Func0>() {
@Override
public Observable
其中i是类的成员变量,运行结果如下:
just result:10
defer result:15
可以看到,just操作符是在创建Observable就进行了赋值操作,而defer是在订阅者订阅时才创建Observable,此时才进行真正的赋值操作
timer操作符是创建一串连续的数字,产生这些数字的时间间隔是一定的;
timer操作符默认情况下是运行在一个新线程上的,当然你可以通过传入参数来修改其运行的线程.
//每隔两秒产生一个数字
Observable.timer(2, 2, TimeUnit.SECONDS).subscribe(new Subscriber() {
@Override
public void onCompleted() {
System.out.println("Sequence complete.");
}
@Override
public void onError(Throwable e) {
System.out.println("error:" + e.getMessage());
}
@Override
public void onNext(Long aLong) {
System.out.println("Next:" + aLong.toString());
}
});
运行结果如下:
Next:0
Next:1
Next:2
Next:3
……
调用例子就不列出了,基本跟上面timer的调用例子一样。
//产生从3开始,个数为10个的连续数字
Observable.range(3,10).subscribe(new Subscriber() {
@Override
public void onCompleted() {
System.out.println("Sequence complete.");
}
@Override
public void onError(Throwable e) {
System.out.println("error:" + e.getMessage());
}
@Override
public void onNext(Integer i) {
System.out.println("Next:" + i.toString());
}
});
运行结果如下:
Next:3
Next:4
Next:5
Next:6
….
Next:12
Sequence complete.
repeat调用例子如下:
//连续产生两组(3,4,5)的数字
Observable.range(3,3).repeat(2).subscribe(new Subscriber() {
@Override
public void onCompleted() {
System.out.println("Sequence complete.");
}
@Override
public void onError(Throwable e) {
System.out.println("error:" + e.getMessage());
}
@Override
public void onNext(Integer i) {
System.out.println("Next:" + i.toString());
}
});
运行结果如下:
Next:3
Next:4
Next:5
Next:3
Next:4
Next:5
Sequence complete.
//repeatWhen调用例子如下:
Observable.just(1,2,3).repeatWhen(new Func1, Observable>>() {
@Override
public Observable> call(Observable extends Void> observable) {
//重复3次
return observable.zipWith(Observable.range(1, 3), new Func2() {
@Override
public Integer call(Void aVoid, Integer integer) {
return integer;
}
}).flatMap(new Func1>() {
@Override
public Observable> call(Integer integer) {
System.out.println("delay repeat the " + integer + " count");
//1秒钟重复一次
return Observable.timer(1, TimeUnit.SECONDS);
}
});
}
}).subscribe(new Subscriber() {
@Override
public void onCompleted() {
System.out.println("Sequence complete.");
}
@Override
public void onError(Throwable e) {
System.err.println("Error: " + e.getMessage());
}
@Override
public void onNext(Integer value) {
System.out.println("Next:" + value);
}
});
运行结果如下:
Next:1
Next:2
Next:3
repeat the 1 count
Next:1
Next:2
Next:3
repeat the 2 count
Next:1
Next:2
Next:3
repeat the 3 count
Next:1
Next:2
Next:3
Sequence complete.