一:創(chuàng)建操作符
1.create:建議你在傳遞給create方法的函數(shù)中檢查觀察者的isUnsubscribed狀態(tài)闪彼,以便在沒(méi)有觀察者的時(shí)候续崖,讓你的Observable停止發(fā)射數(shù)據(jù)或者做昂貴的運(yùn)算商佛。
Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> e) throws Exception {
if (!e.isDisposed()){
e.onNext("1");
e.onNext("2");
e.onNext("3");
}
e.onComplete();
}
}).subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String s) {
Log.d(TAG,s);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
2.Defer :操作符會(huì)一直等待直到有觀察者訂閱它慎冤,然后它使用Observable工廠方法生成一個(gè)Observable。它對(duì)每個(gè)觀察者都這樣做虏缸,因此盡管每個(gè)訂閱者都以為自己訂閱的是同一個(gè)Observable鲫懒,事實(shí)上每個(gè)訂閱者獲取的是它們自己的單獨(dú)的數(shù)據(jù)序列。
在某些情況下刽辙,等待直到最后一分鐘(就是知道訂閱發(fā)生時(shí))才生成Observable可以確保Observable包含最新的數(shù)據(jù)窥岩。
public class CourseThreeActivity extends AppCompatActivity {
private final static String TAG = "CourseThreeActivity";
private int num = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_three);
Observable<Integer> observable = Observable.just(num);
num =20;
Subscriber<Integer> subscriber = new Subscriber<Integer>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Integer integer) {
Log.e(TAG,integer+"");
}
};
observable.subscribe(subscriber);
}
}
運(yùn)行結(jié)果如下
20244-20244/com.pse.rxandroid E/CourseThreeActivity: 10
可見(jiàn)當(dāng)我們調(diào)用Just 時(shí)該Observerable 的數(shù)據(jù)已經(jīng)固定。即使在下面做出更改也不會(huì)造成影響宰缤。
那我們?cè)賮?lái)看看defer
public class CourseThreeActivity extends AppCompatActivity {
private final static String TAG = "CourseThreeActivity";
private int num = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_three);
Observable<Integer> observable = Observable.defer(new Func0<Observable<Integer>>() {
@Override
public Observable<Integer> call() {
return Observable.just(num);
}
});
num =20;
Subscriber<Integer> subscriber = new Subscriber<Integer>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Integer integer) {
Log.e(TAG,integer+"");
}
};
observable.subscribe(subscriber);
}
}
運(yùn)行結(jié)果如下
21658-21658/com.pse.rxandroid E/CourseThreeActivity: 20
現(xiàn)在想必大家能夠理解defer 的作用了吧(等待訂閱颂翼,獲取最新值)。
3.From :將其它種類的對(duì)象和數(shù)據(jù)類型轉(zhuǎn)換為Observable
使用方法如下:
Integer[] items = { 0, 1, 2, 3, 4, 5 };
Observable myObservable = Observable.from(items);
myObservable.subscribe(
new Action1<Integer>() {
@Override
public void call(Integer item) {
System.out.println(item);
}
},
new Action1<Throwable>() {
@Override
public void call(Throwable error) {
System.out.println("Error encountered: " + error.getMessage());
}
},
new Action0() {
@Override
public void call() {
System.out.println("Sequence complete");
}
}
);
結(jié)果大家可想而知慨灭,不過(guò)在這里有一個(gè)新的操作符Action0朦乏,其實(shí)看過(guò)源碼就會(huì)知道Action可以有多個(gè)參數(shù),方便我們來(lái)重寫氧骤,處理自己的需求呻疹,這里我們只是模擬Observer創(chuàng)建了 onNext(),onError(),onComplete().
4.Range:創(chuàng)建一個(gè)發(fā)射特定整數(shù)序列的Observable
示例代碼如下
Observable.range(5,10).subscribe(new Action1<Integer>() {
@Override
public void call(Integer integer) {
Log.e(TAG,integer+"");
}
});
這里我們只是為了測(cè)試,所以只處理onNext()事件筹陵。
運(yùn)行結(jié)果如下
03-07 14:01:34.029 7672-7672/com.pse.rxandroid E/CourseThreeActivity: 5
03-07 14:01:34.029 7672-7672/com.pse.rxandroid E/CourseThreeActivity: 6
03-07 14:01:34.029 7672-7672/com.pse.rxandroid E/CourseThreeActivity: 7
03-07 14:01:34.029 7672-7672/com.pse.rxandroid E/CourseThreeActivity: 8
03-07 14:01:34.029 7672-7672/com.pse.rxandroid E/CourseThreeActivity: 9
03-07 14:01:34.029 7672-7672/com.pse.rxandroid E/CourseThreeActivity: 10
03-07 14:01:34.029 7672-7672/com.pse.rxandroid E/CourseThreeActivity: 11
03-07 14:01:34.029 7672-7672/com.pse.rxandroid E/CourseThreeActivity: 12
03-07 14:01:34.029 7672-7672/com.pse.rxandroid E/CourseThreeActivity: 13
03-07 14:01:34.029 7672-7672/com.pse.rxandroid E/CourseThreeActivity: 14
5.Repeat :創(chuàng)建一個(gè)發(fā)射特定數(shù)據(jù)重復(fù)多次的Observable
Observable<String> observable = Observable.just("hell Word");
observable.repeat(4).subscribe(new Action1<String>() {
@Override
public void call(String s) {
Log.e(TAG,s);
}
});
運(yùn)行結(jié)果如下,會(huì)連續(xù)發(fā)射4四次Observerable(同一個(gè))
03-07 14:13:11.723 17500-17500/com.pse.rxandroid E/CourseThreeActivity: hell Word
03-07 14:13:11.725 17500-17500/com.pse.rxandroid E/CourseThreeActivity: hell Word
03-07 14:13:11.725 17500-17500/com.pse.rxandroid E/CourseThreeActivity: hell Word
03-07 14:13:11.725 17500-17500/com.pse.rxandroid E/CourseThreeActivity: hell Word
6.Timer:創(chuàng)建一個(gè)Observerable刽锤,并在一定的延時(shí)后發(fā)射一個(gè)特定的值“0”
示例如下
Observable<Long> observable = Observable.timer(2, TimeUnit.SECONDS);
observable.subscribe(new Action1<Long>() {
@Override
public void call(Long aLong) {
Log.e(TAG,aLong+"");
}
});
運(yùn)行結(jié)果如下
26279-26302/com.pse.rxandroid E/CourseThreeActivity: 0
在日常中我們經(jīng)常做倒計(jì)時(shí)操作,之前使用的是Handler 現(xiàn)在你可以嘗試改用RX實(shí)現(xiàn)了惶翻。
7.MAP():最重要的變換操作符姑蓝,對(duì)Observable發(fā)射的每一項(xiàng)數(shù)據(jù)應(yīng)用一個(gè)函數(shù),執(zhí)行變換操作
大家可以試著理解下這個(gè)圖:
上面這張圖我可以舉個(gè)例子吕粗,比如幼兒園舉行活動(dòng)纺荧,我們要給每個(gè)孩子穿上禮服,那么這個(gè)map()就是我們換衣服這個(gè)方法,所有的孩子經(jīng)過(guò)map()后都會(huì)穿上華麗的禮服宙暇。這樣的方式我們就會(huì)很快理解上圖的意義输枯。
示例如下
Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
Log.e(TAG,1+"");
subscriber.onNext(1);
subscriber.onCompleted();
}
}).map(new Func1<Integer, String>() {
@Override
public String call(Integer integer) {
return integer+"||";
}
}).subscribe(new Action1<String>() {
@Override
public void call(String s) {
Log.e(TAG,s);
}
});
03-07 14:38:08.358 7900-7900/? E/CourseThreeActivity: 1
03-07 14:38:08.358 7900-7900/? E/CourseThreeActivity: 1||
從上面看出來(lái),我們發(fā)射的數(shù)據(jù)是整數(shù)類型占贫,但經(jīng)過(guò)Map()后就變?yōu)镾tring類型桃熄,這個(gè)操作符默認(rèn)不在任何特定的調(diào)度器上執(zhí)行。(有序)
8.FlatMap():將一個(gè)發(fā)射數(shù)據(jù)的Observable變換為多個(gè)Observables型奥,然后將它們發(fā)射的數(shù)據(jù)合并后放進(jìn)一個(gè)單獨(dú)的Observable,聽(tīng)著有點(diǎn)繞口瞳收,那么我們看下示例
我們先在頁(yè)面建立以下代碼
Observable.just(1, 2, 3, 4, 5, 6, 7)
.flatMap(new Func1<Integer, Observable<String>>() {
@Override
public Observable<String> call(Integer integer) {
return getObserverable(integer);
}
}).subscribe(
new Action1<String>() {
@Override
public void call(String s) {
Log.e(TAG,s);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
Log.e(TAG,throwable.getMessage());
}
}
);
private Observable<String> getObserverable(final int id) {
return Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
subscriber.onNext("This is the" + id);
subscriber.onCompleted();
}
});
}
結(jié)果如下
om.pse.rxandroid E/CourseThreeActivity: This is the1
om.pse.rxandroid E/CourseThreeActivity: This is the2
om.pse.rxandroid E/CourseThreeActivity: This is the3
om.pse.rxandroid E/CourseThreeActivity: This is the4
om.pse.rxandroid E/CourseThreeActivity: This is the5
om.pse.rxandroid E/CourseThreeActivity: This is the6
om.pse.rxandroid E/CourseThreeActivity: This is the7
9.Merge合并多個(gè)Observables的發(fā)射物
示例代碼如下
Observable<Integer> odds = Observable.just(1, 3, 5).subscribeOn(someScheduler);
Observable<Integer> evens = Observable.just(2, 4, 6);
Observable.merge(odds, evens)
.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.");
}
});
運(yùn)行結(jié)果如下
Next: 1
Next: 3
Next: 5
Next: 2
Next: 4
Next: 6
Sequence complete.
以上是我們?nèi)粘i_(kāi)發(fā)要用到的一些操作符,但是RxJava 可不止這些厢汹,如有需要可以去這里學(xué)習(xí)螟深。
RxJava中文學(xué)習(xí)文檔