Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RxJava 入門

RxJava 入門

RxJava を使っていない人向けの RxJava の入門解説です。

以下について説明します。

- Rx とは
- Observable, Observer, subscribe, Disposable, Operator
- よく使う Operator
- 簡単なデバッグのしかた

Hiroshi Kurokawa

June 29, 2017
Tweet

More Decks by Hiroshi Kurokawa

Other Decks in Programming

Transcript

  1. ؎كٝزח⿾䘔ׅ׷׌ֽ# service.contributors(new NetworkCall.Listener<List<Contributor >>() {
 @Override public void success(List<Contributor> data)

    {
 runOnUiThread(() -> {
 if (!isDestroyed()) {
 adapter.setContributors(data);
 }
 });
 }
 
 @Override public void failure(Throwable t) {}
 }); 3Yך؝خ  أٖحس䭷㹀 ٓ؎ؿ؟؎ؙٕثؑحؙ
  2. 3Yך嚊䙀 { Observable # Observer subscribe emit emit emit Event

    subscribeঢ়گΛ؅ཧ͢Δ΋ͷ = Disposable
  3. 0CTFSWBCMF ‣ &WFOU׾וֲFNJUׅ׷ַ׾㹀׭׷ ‣ 荈ⴓד⡲׷ֿהכ֮ת׶זְ ‣ طحزٙ٦ؙ鸐⥋̔3FUSPU ‣ 6*؎كٝز̔3Y#JOEJOH //

    1ඵ͝ͱʹLongܕͷΠϕϯτ͕emit͞ΕΔʢ1, 2, 3, …ʣ Observable.interval(1, TimeUnit.SECONDS); 
 // Ϧετͷཁૉ͕1ͭͣͭemit͞ΕΔ Observable.fromIterable(list);
  4. 0CTFSWFS service.contributors(owner, repository)
 .subscribe(new Observer<List<Contributor >>() {
 @Override public void

    onSubscribe(Disposable d) {
 }
 
 @Override public void onNext(List<Contributor> contributors) {
 }
 
 @Override public void onError(Throwable e) {
 }
 
 @Override public void onComplete() {
 }
 });
 TVCTDSJCFׁ׸׋ FWFOU׾「ֽ《׏׋ ؒٓ٦ָ饯ֹ׋ ֿ׸⟃♳FWFOUָזְ
  5. service.contributors(owner, repository)
 .subscribe(new DefaultObserver<List<Contributor >>() {
 @Override public void onNext(List<Contributor>

    contributors) {
 ‌}
 
 @Override public void onError(Throwable e) {
 ‌}
 
 @Override public void onComplete() {
 ‌}
 });
 0CTFSWFS
  6. %JTQPTBCMF @Override protected void onCreate(Bundle savedInstanceState) {
 …
 disposable =

    service.contributors(owner, repository)
 .subscribe(
 contributors -> {},
 throwable -> {},
 () -> {}
 ); }
 
 @Override protected void onDestroy() {
 super.onDestroy();
 if (disposable != null) {
 disposable.dispose();
 }
 } ֿ׸⟃꣬כ؎كٝز׾「ֽ《׵זְ
  7. ״ֻ֮׷⹞麩ְ ‣ 0CTFSWBCMF׾⡲׏׋׵ծּׅחFWFOUָFNJUׁ׸׷ ‣ 0CTFSWBCMFָⰟ剣ׁ׸׷ Observable<List<Contributor >> o = service.contributors();

    o.subscribe(contributors -> {}, throwable -> {}, () -> {}); o.subscribe(contributors -> {}, throwable -> {}, () -> {}); طحزٙ٦ؙ鸐⥋ָ饯ֹ׷さ鎘㔐
  8. رغحؚך؝خ ▸ EP0O&BDIؔلٖ٦ة׾ⵃ欽ׅ׷ service.contributors(owner, repository)
 .doOnEach(event -> Log.d(TAG, "event: "

    + event))
 .flatMap(…) .map(…)
 … MainActivity D notification: OnNextNotification[[…]] MainActivity D notification: OnCompleteNotification