RxJava 入門

RxJava 入門

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

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

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

979d93b360f80486b121486a9d063ad5?s=128

Hiroshi Kurokawa

June 29, 2017
Tweet

Transcript

  1. RxJava ೖ໳ Droid Girls Meetup #6 ࠇ઒ ༸ (@hydrakecat)

  2. 3Y+BWBהכ ▸ 3Y 3FBDUJWF&YUFOTJPO ך+BWB㹋鄲 ▸ ➭ח׮$װ+4זוד㹋鄲ָ֮׷ ▸ 嚊䙀װ欽铂ָ♧筰זךד鎉铂ָ㢌׻׏ג׮椚鍑׃ װְׅ

  3. 3Y+BWBהכ ▸ ꬊず劍Ⳣ椚׾䪔ֲ׋׭ךٓ؎ـٓٔ ▸ طحزٙ٦ؙ鸐⥋ծ%#㉏ְさ׻ׇ ▸ 6*؎كٝز

  4. 3Y+BWB ▸ 3Y+BWBכY禸הY禸ד㣐ֹֻ麩ְתׅ ▸ 欽铂ծⵖꣲծؔلٖ٦ةזו ▸ ➙㔐כ3Y+BWB׾⢪ְתׅ ▸ ָծ׉ֿתד麩ְ׾孡ח׃זֻג㣐♱㣗דׅ

  5. 3Yך嚊䙀

  6. ؎كٝزח⿾䘔ׅ׷׌ֽ# 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ך؝خ  أٖحس䭷㹀 ٓ؎ؿ؟؎ؙٕثؑحؙ
  7. ؎كٝزח⿾䘔ׅ׷׌ֽ# 3Yך؝خ  service.contributors()
 …
 .subscribe(
 data -> adapter.setContributors(data),
 throwable

    -> Log.e(TAG, throwable.getMessage())
 ); וֲ⿾䘔ׅ׷ַ׌ֽ剅ֻ
  8. ؎كٝزח⿾䘔ׅ׷׌ֽ# 3Yך؝خ  service.contributors()
 …
 .subscribe(
 data -> adapter.setContributors(data),
 throwable

    -> Log.e(TAG, throwable.getMessage())
 ); ֿֿחאְגכ䖓鶢
  9. 3Yך嚊䙀 { Observable # Observer subscribe emit emit emit Event

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

    1ඵ͝ͱʹLongܕͷΠϕϯτ͕emit͞ΕΔʢ1, 2, 3, …ʣ Observable.interval(1, TimeUnit.SECONDS); 
 // Ϧετͷཁૉ͕1ͭͣͭemit͞ΕΔ Observable.fromIterable(list);
  11. 0CTFSWFS ‣ ؎كٝز׾「ֽ《׏גוֲ⿾䘔ׅ׷ַ׾㹀׭׷ ‣ 0CTFSWBCMFTVCTDSJCF ך䒷侧ח♷ִ׷ ‣ ؎كٝز׀הח؝٦ٕغحًؙاحسָㄎל׸׷

  12. 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ָזְ
  13. 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
  14. service.contributors(owner, repository)
 .subscribe( contributors -> {},
 throwable -> {},
 ()

    -> {} );
 
 0CTFSWFS
  15. 圫ղז0CTFSWBCMF ‣ PO/FYU ָ⡦㔐׮ㄎל׸גPO$PNQMFUFE כ宕黅 חㄎל׸זְ ‣ PO/FYU ָ㔐׌ֽㄎל׸ג湫䖓חPO$PNQMFUFE DPNQMFUFE

  16. 圫ղז0CTFSWBCMF ‣ PO/FYU ָ㔐׮ㄎל׸׆חPO&SSPS ָㄎל׸׷ FSSPS

  17. %JTQPTBCMF ‣ TVCTDSJCFך朐䡾׾盖椚 ‣ 0CTFSWBCMFTVCTDSJCF ך䨱׶⦼ ‣ %JTQPTBCMFEJTQPTF ׾ㄎעהծ
 ׉׸⟃꣬כ0CTFSWFSכ؎كٝز׾「ֽ《׵זְ

  18. %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();
 }
 } ֿ׸⟃꣬כ؎كٝز׾「ֽ《׵זְ
  19. %JTQPTBCMF ‣ "OESPJEדכTVCTDSJCFהEJTQPTFך㜥䨽ָٓ؎ ؿ؟؎ؙٕ♳ד㼎獥חז׷״ֲחׅ׷ ‣ TVCTDSJCFPO$SFBUF EJTQPTFPO%FTUSPZ  ‣ TVCTDSJCFPO4UBSU

    EJTQPTFPO4UPQ  ‣ TVCTDSJCFPO"UUBDIFE5P8JOEPX 
 EJTQPTFPO%FUBDIFE'SPN8JOEPX
  20. 3Yך؝خ  0CTFSWBCMF׾㢌䳔׃גְֻؔلٖ٦ة׾黝欽ׅ׷ ֿהדծ実׭׷0CTFSWBCMFחׅ׷

  21. تـٕةحف׾嗚⳿׃׋ְ = Operator 0QFSBUPS ྡΓ߹͏Πϕϯτ͕Ұఆ࣌ؒҎ಺
 ͩͬͨΒΠϕϯτΛemit͢ΔΑ͏ͳObservable
 ʹม׵͢Δ

  22. 圫ղז0QFSBUPS ▸ MUFS ▸ 勴⟝ח֮׏׋؎كٝز׌ֽ鸐ׅ

  23. 圫ղז0QFSBUPS ▸ UBLF ▸ ؎كٝز׾⯓걧ַ׵䭷㹀ׁ׸׋侧׌ֽ鸐ׅ

  24. 圫ղז0QFSBUPS ▸ NBQ ▸ ؎كٝز׾㢌䳔ׅ׷

  25. 圫ղז0QFSBUPS ▸ BU.BQ ▸ ؎كٝزאאַ׵倜׋ח0CTFSWBCMF׾⡲ ׶ծ׉ַֿ׵FNJUׁ׸׷؎كٝز׾תה׭׷

  26. NBQהBU.BQך⢪ְⴓֽ ‣ ♧菙涸ח㢌䳔䖓ך؎كٝزָ0CTFSWBCMFחז׷ז ׵BU.BQ׾⢪ֲ ‣ ⢽Ⰵ׸㶨ךطحزٙ٦ؙ鸐⥋ ‣ ⢽ٔأز׾㾜Ꟛׅ׷ service.items()
 .flatMap(item

    -> service.user(item.getUser())) service.usersList()
 .flatMap(users -> Observable.fromIterable(users))
  27. NBQהBU.BQך⢪ְⴓֽ ‣ ׉ֲדזְז׵NBQ׾⢪ֲ ‣ ⢽ٔأزך"EBQUFS欽חؔـآؙؑز׾㢌䳔 service.users()
 .map(user -> new Contributor(user.getLogin(),

    user.getName()))
  28. 圫ղז0QFSBUPS ▸ DPNCJOF-BUFTU ▸ 醱侧ך0CTFSWBCMFךֲ׍ծוַָֿ؎كٝز׾ FNJU׃׋׵ծ➭ך0CTFSWBCMFך剑倜ך؎كٝ زהさ׻ׇ׋؎كٝز׾FNJUׅ׷

  29. 圫ղז0QFSBUPS ▸ DPNCJOF-BUFTU

  30. 圫ղז0QFSBUPS ▸ TVCTDSJCF0O ▸ ؎كٝز׾FNJUׅ׷הֹךأٖحس׾㢌刿ׅ׷ service.contributors(owner, repository)
 .subscribeOn(Schedulers.io())
 طحزٙ٦ؙ鸐⥋׾ً؎ٝأٖحسהⴽךأٖحسד遤ֲ

  31. 圫ղז0QFSBUPS ▸ TVCTDSJCF0O ▸ ؎كٝز׾FNJUׅ׷הֹךأٖحس׾㢌刿ׅ׷ ▸ 姻然חכ׍׳׏ה麩ְתָׅծה׶ִ֮׆כֿ ְֲֲ椚鍑ד$

  32. 圫ղז0QFSBUPS ▸ PCTFSWF0O ▸ ؎كٝز׾「ֽ《׷הֹךأٖحس׾㢌刿ׅ׷ service.contributors(owner, repository)
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread()) .subscribe(contributors

    -> { … }) TVCTDSJCF ⰻד遤ֲⳢ椚׾ً؎ٝأٖحسד遤ֲ
  33. 圫ղז0QFSBUPS ▸ GSPN*UFSBCMF ▸ ٔأز*UFSBCMFַ׵0CTFSWBCMF׾⡲׷ FromIterable

  34. 圫ղז0QFSBUPS ▸ UP-JTU ▸ ؎كٝز׾Ⰻ鿇תה׭ג-JTUחׅ׷ toList

  35. 3Yך؝خ  ▸ ؿؑ٦أחⴓֽג罋ִ׷ ▸ ر٦ةך崧׸׾ת׆㹀纏 ▸ 0CTFSWBCMF׾欽䠐׃׋׶ծPQFSBUPS׾籬ְד וֲ㢌䳔ׅ׷ַ䭷爙ׅ׷ ▸

    ر٦ة׾崧ׅ ▸ TVCTDSJCFׅ׷
  36. Pipes by Domiriel CC BY-NC 2.0

  37. ״ֻ֮׷⹞麩ְ ‣ 0CTFSWBCMF׾⡲׏׋׵ծּׅחFWFOUָFNJUׁ׸׷ Observable<List<Contributor >> o = service.contributors(); ת׌طحزٙ٦ؙ鸐⥋כ
 饯ֹזְ

  38. ״ֻ֮׷⹞麩ְ ‣ 0CTFSWBCMF׾⡲׏׋׵ծּׅחFWFOUָFNJUׁ׸׷ Observable<List<Contributor >> o = service.contributors(); o.subscribe(contributors ->

    {}, throwable -> {}, () -> {}); طحزٙ٦ؙ鸐⥋ָ饯ֹ׷
  39. ״ֻ֮׷⹞麩ְ ‣ 0CTFSWBCMF׾⡲׏׋׵ծּׅחFWFOUָFNJUׁ׸׷ ‣ 0CTFSWBCMFָⰟ剣ׁ׸׷ Observable<List<Contributor >> o = service.contributors();

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

    + event))
 .flatMap(…) .map(…)
 … MainActivity D notification: OnNextNotification[[…]] MainActivity D notification: OnCompleteNotification
  41. ➙傈鑧ׁזַ׏׋ֿה ▸ 4JOHMF .BZCF $PNQMFUBCMF ▸ )PU0CTFSWBCMFה$PME0CTFSWBCMF ▸ 4VCKFDU ▸

    'MPXBCMFה0CTFSWBCMFך麩ְ ▸ #BDLQSFTTVSFהכ ▸ ذأز
  42. ⿫罋俑柃 ‣ IUUQSFBDUJWFYJP ‣ IUUQSYXJLJXJLJEPUDPNTBNQMFT ‣ ˑ3FBDUJWF1SPHSBNNJOHXJUI3Y+BWB$SFBUJOH "TZODISPOPVT &WFOU#BTFE"QQMJDBUJPOT˒ 

    5PNBT[/VSLJFXJD[ #FO$ISJTUFOTFO