-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeferOperator.java
42 lines (31 loc) · 1.13 KB
/
DeferOperator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package rxjava.creating;
import java.util.concurrent.Callable;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import common.CommonIntegerObserver;
/**
* Author: andy.xwt
* Date: 2019/1/31 12:10
* Description:直到有观察者订阅时,才动态的创建被观察者
* 延迟操作符等待直到观察者订阅它,在某些情况下,等到最后一分钟(即订阅时间)生成Observable可以确保该Observable包含最新的数据。
*
* @see <a href= "https://mcxiaoke.gitbooks.io/rxdocs/content/operators/Defer.html"/>
*/
class DeferOperator {
static int i = 0;
private static void test() {
Observable<Integer> defer = Observable.defer(new Callable<ObservableSource<Integer>>() {
@Override
public ObservableSource<Integer> call() throws Exception {
return Observable.just(i);
}
});
System.out.println("订阅之间i的值---->" + i);
//打印订阅后改变的值
i = 10;
defer.subscribe(new CommonIntegerObserver());
}
public static void main(String[] args) {
test();
}
}