RxJava基本使用2

2021-08-14 07:30:04 字數 1101 閱讀 2549

map變換操作符:將傳送的資料按指定的函式去變化

例子:

observable.create(new observableonsubscribe() 

}).map(new function()

}).subscribe(new consumer()

});

flatmap變換操作符:將傳送的事件分解成多個事件後發送回接收方

注意:flatmap並不保證事件的順序,如需保證時間順序使用concatmap,使用方法和flatmap一樣。

例子:

observable.create(new observableonsubscribe() 

}).flatmap(new function>()

}).subscribe(new consumer()

});

結果:

zip:類似於flatmap的逆過程,將多個事件合為乙個事件。

例子:

observable observable1 = observable.create(new observableonsubscribe() 

});observable observable2 = observable.create(new observableonsubscribe()

});observable.zip(observable1, observable2, new bifunction()

}).subscribe(new consumer()

});

結果:

發現observable1傳送完成後,observable才開始傳送,因為兩個傳送方在同一執行緒中,可各自呼叫subscribeon方法,讓其執行在不同執行緒上。

Rxjava基本用法總結

參考 扔物線大神 1.observable 被觀察者 有兩種建立方式 observable observable observable.create new observableonsubscribe observable observable1 observable.just hello hi a...

RxJava2簡單使用三(執行緒排程)

之前我也用到過執行緒排程,但是沒有仔細講這個問題,我這裡作為新手還是解釋一下 執行緒排程一 基本配置 subscribeon schedulers.newthread 將被觀察者設定在乙個新的執行緒來執行 observeon androidschedulers.mainthread 將觀察者設定在主...

RxJava2原始碼解析

原始碼總結 observabel 通過create方法。將observableonsubscribe物件傳遞給自己。通過subscribe方法。建立 observableemitter發射器物件。發射器裡又封裝了observer。發射器又作為引數傳遞 給observableonsubscribe物件...