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

RxSwift / RxJava の基本と実践

RxSwift / RxJava の基本と実践

アジェンダ
----
はじめに
Rx を使う動機
基本 30
- Observable はストリーム
- Observable を変換する
- subscribe が大事
- ストリームとデータの流れ
- イベントの種類
- Disposable
発展 10
- マルチスレッド
- ストリーム結合・分割
- Hot / Cold

M.Inomata

July 18, 2018
Tweet

More Decks by M.Inomata

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ גࣜձࣾ tech vein ୅ද ழມ ॆԝ (͍ͷ·ͨ ΈͭͻΖ) ձࣾHP:

    https://www.tech-vein.com/ private twitter: @ino2222
  2. ݸਓతͳࣗݾ঺հ www.ecoop.net ͱ͍͏αΠτΛ2002೥͔ΒӡӦ͍ͯ͠·͢ɻ GNU Make ͱ͍͏ίϚϯυͷӳޠυΩϡϝϯτΛͻͱΓͰίπίπຊҰ࡭෼຋༁ɺ2002೥ʹެ։ͨ͠Γ͠ ·ͨ͠ɻ ࠓ͸ओʹ iOSΞϓϦ։ൃऀΛ͍ͯ͠·͢ɻ WindowsΞϓϦ։ൃ(2000~2003)

    → PHP։ൃ/Πϯϑϥӡ༻(2003~2012) ˍ Java։ൃ(ΨϥέʔΞϓϦˍJavaEE)(2005~2010) → AndroidΞϓϦ։ൃ(2010~2013) → iPhone ΞϓϦ։ൃ(2013~) ʴ α AndroidʹະདྷΛײͨ͡2010೥ʹΞϓϦΛॳΊͯެ։͠ɺ௨ࢉ15ສμ΢ϯϩʔυΛୡ੒ʂ …͢Δ΋ɺެ։̍िؒͰߋ৽༻ͷ伴Λ๨ΕͯϝϯςφϯεෆՄೳʹ…ɻ
  3. ͸͡Ίʹ • αϯϓϧίʔυ͸͢΂ͯ RxSwift Ͱॻ͔Ε͍ͯ ·͢ɻ • RxJava ΍ RxJSͰ΋جຊ͸ಉ͡Ͱ͕͢ɺࡉ͔

    ͍จ๏ɺਖ਼֬ͳϝιου໊͸ҧ͏͜ͱ͕͋Γ ·͢ɻಉ͡༻్ͷϝιου͕͋ΔͷͰɺ࢖͏ ͱ͖͸ஔ͖׵͑ͯԼ͍͞ɻ
  4. RxSwift/RxJavaͱ͸ • ֤ݴޠͰ࣮૷͞Ε͍ͯΔʮRx(Reactive Extension)ʯ
 ͷSwift, Java ޲͚ϥΠϒϥϦ • Reactive Extension

    … ΋ͱ΋ͱ͸
 C#.NET Ͱ࢖ΘΕͩͨ͠ Rx.NET ͕ΦϦδφϧ • JavaScript(RxJS), C++(RxCpp), Ruby(Rx.rb), PHP(RxPHP) ͳͲͳͲଟ਺
  5. “Hello” “Rx” “World” Observable<String> completed ࣌ؒͷྲྀΕ ͜ͷਤΛʮϚʔϒϧμΠΞάϥϜʯͱ͍͍ɺ ̍ͭͷ໼ҹ͕̍ͭͷετϦʔϜ 0CTFSWBCMF Λද͠·͢ɻ

    ࠨ͔Βॱ൪ʹΠϕϯτ͕ྲྀΕΔΠϝʔδͰ͢ɻ ̍ͭͷʮ˔ʯ͕̍ͭͷཁૉ͕དྷͨ OFYU ΠϕϯτΛද͠ɺ ʮʛʯ͕ऴྃ DPNQMFUFE ΠϕϯτΛද͠·͢ɻ ͜ͷσʔλͷྲྀΕ͕ ͭͷετϦʔϜ ʹ̍ͭͷ0CTFSWBCMF
  6. Observable͸࡞੒ɾม׵Ͱ͖Δ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .map { $0 * 2 }      <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ 0CTFSWBCMF*OU <*OU> 1 completed 2 3 4 5 ετϦʔϜ࡞੒
  7. Observable͸࡞੒ɾม׵Ͱ͖Δ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .map { $0 * 2 }         <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ ح਺͚ͩ࢒͢ϑΟϧλΛ͔͚Δ 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> 1 completed 2 3 4 5 1 completed 3 5 ετϦʔϜม׵
  8. Observable͸࡞੒ɾม׵Ͱ͖Δ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .map { $0 * 2 }            <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ ح਺͚ͩ࢒͢ϑΟϧλΛ͔͚Δ ̎ഒͷ஋ʹม׵͢Δ 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> 1 completed 2 3 4 5 1 completed 3 5 2 completed 6 10 ετϦʔϜม׵
  9. Observable .from([1,2,3,4,5]) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ 0CTFSWBCMF*OU <*OU>

    ετϦʔϜ 0CTFSWBCMF Λ ฦ͚͢Ͳʜ subscribe͠ͳ͍ͱετϦʔϜ͸࢝·Βͳ͍
  10. Observable .from([1,2,3,4,5]) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ 0CTFSWBCMF*OU <*OU>

    σʔλ͸ྲྀΕͯ͜ͳ͍ʜ ƅшƅ ŴƅŜŖƃʜ subscribe͠ͳ͍ͱετϦʔϜ͸࢝·Βͳ͍
  11. Observable .from([1,2,3,4,5]) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ 0CTFSWBCMF*OU <*OU>

    .subscribe(onNext: { print(“next: \($0)”) }) ։࢝ͯ͠ॳΊͯඇಉظͰॱ൪ʹσʔλ͕ྲྀΕ·͢ɻ ˞Ұ෦ )PU0CTFSWBCMF Λআ͘ ƅшƅ Űŕ ಥવͷTVCTDSJCFʂ subscribe͠ͳ͍ͱετϦʔϜ͸࢝·Βͳ͍
  12. subscribeͰετϦʔϜΛ։࢝͢Δ Observable .from([1,2,3,4,5]) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ 0CTFSWBCMF*OU

    <*OU> .subscribe(onNext: { print(“next: \($0)”) }) lOFYUz ਺஋Λϩάग़ྗ͢Δ ετϦʔϜͷ։࢝ ։࢝ͯ͠ॳΊͯඇಉظͰॱ൪ʹσʔλ͕ྲྀΕ·͢ɻ ˞Ұ෦ )PU0CTFSWBCMF Λআ͘
  13. subscribeͰετϦʔϜΛ։࢝͢Δ Observable .from([1,2,3,4,5]) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ 0CTFSWBCMF*OU

    <*OU> .subscribe(onNext: { print(“next: \($0)”) }) lOFYUz ਺஋Λϩάग़ྗ͢Δ ετϦʔϜ͔ΒΠϕϯτ σʔλ ͕ྲྀΕΔ  ։࢝ͯ͠ॳΊͯඇಉظͰॱ൪ʹσʔλ͕ྲྀΕ·͢ɻ ˞Ұ෦ )PU0CTFSWBCMF Λআ͘
  14. subscribeͰετϦʔϜΛ։࢝͢Δ Observable .from([1,2,3,4,5]) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ 0CTFSWBCMF*OU

    <*OU> .subscribe(onNext: { print(“next: \($0)”) }) lOFYUz ਺஋Λϩάग़ྗ͢Δ ετϦʔϜ͔ΒΠϕϯτ σʔλ Λड͚औΔ ˠlOFYUz  Λग़ྗ͢Δɻ  ։࢝ͯ͠ॳΊͯඇಉظͰॱ൪ʹσʔλ͕ྲྀΕ·͢ɻ ˞Ұ෦ )PU0CTFSWBCMF Λআ͘
  15. subscribeͰετϦʔϜΛ։࢝͢Δ Observable .from([1,2,3,4,5]) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ 0CTFSWBCMF*OU

    <*OU> .subscribe(onNext: { print(“next: \($0)”) }) lOFYUz ਺஋Λϩάग़ྗ͢Δ ˠlOFYUz  Λग़ྗ͢Δɻ   ετϦʔϜ͔ΒΠϕϯτ σʔλ ͕ྲྀΕΔ ։࢝ͯ͠ॳΊͯඇಉظͰॱ൪ʹσʔλ͕ྲྀΕ·͢ɻ ˞Ұ෦ )PU0CTFSWBCMF Λআ͘
  16. subscribeͰετϦʔϜΛ։࢝͢Δ Observable .from([1,2,3,4,5]) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ 0CTFSWBCMF*OU

    <*OU> .subscribe(onNext: { print(“next: \($0)”) }) lOFYUz ਺஋Λϩάग़ྗ͢Δ ˠlOFYUz lOFYUz  Λग़ྗ͢Δɻ   ετϦʔϜ͔ΒΠϕϯτ σʔλ Λड͚औΔ ։࢝ͯ͠ॳΊͯඇಉظͰॱ൪ʹσʔλ͕ྲྀΕ·͢ɻ ˞Ұ෦ )PU0CTFSWBCMF Λআ͘
  17. subscribeͰετϦʔϜΛ։࢝͢Δ Observable .from([1,2,3,4,5]) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ 0CTFSWBCMF*OU

    <*OU> .subscribe(onNext: { print(“next: \($0)”) }) lOFYUz ਺஋Λϩάग़ྗ͢Δ ˠlOFYUz lOFYUz  Λग़ྗ͢Δɻ ։࢝ͯ͠ॳΊͯඇಉظͰॱ൪ʹσʔλ͕ྲྀΕ·͢ɻ   தུ
  18. subscribeͰετϦʔϜΛ։࢝͢Δ Observable .from([1,2,3,4,5]) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ 0CTFSWBCMF*OU

    <*OU> .subscribe(onNext: { print(“next: \($0)”) }) lOFYUz ਺஋Λϩάग़ྗ͢Δ ˠlOFYUz lOFYUz ʜ lOFYUz Λग़ྗ͢Δɻ ։࢝ͯ͠ॳΊͯඇಉظͰॱ൪ʹσʔλ͕ྲྀΕ·͢ɻ     
  19. subscribe͢Δ·Ͱ΍ͬͺΓσʔλ͸ྲྀΕͳ͍ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .map { $0 * 2 } <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ ح਺͚ͩ࢒͢ϑΟϧλΛ͔͚Δ ̎ഒͷ஋ʹม׵͢Δ 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> ੩͔ͩʜ ɾТɾA
  20. Observable͸ඇಉظʹॱʹσʔλ͕ྲྀΕΔ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .map { $0 * 2 } .subscribe(onNext: { print(“next: \($0)”) }) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ ح਺͚ͩ࢒͢ϑΟϧλΛ͔͚Δ ̎ഒͷ஋ʹม׵͢Δ lOFYUz ਺஋Λϩάग़ྗ͢Δ 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> ετϦʔϜͷ։࢝
  21. Observable͸ඇಉظʹॱʹσʔλ͕ྲྀΕΔ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .map { $0 * 2 }  .subscribe(onNext: { print(“next: \($0)”) }) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ ح਺͚ͩ࢒͢ϑΟϧλΛ͔͚Δ ̎ഒͷ஋ʹม׵͢Δ lOFYUz ਺஋Λϩάग़ྗ͢Δ 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> 1 completed 2 3 4 5 1 completed 3 5 2 completed 6 10
  22. Observable͸ඇಉظʹॱʹσʔλ͕ྲྀΕΔ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .map { $0 * 2 }   .subscribe(onNext: { print(“next: \($0)”) }) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ ح਺͚ͩ࢒͢ϑΟϧλΛ͔͚Δ ̎ഒͷ஋ʹม׵͢Δ lOFYUz ਺஋Λϩάग़ྗ͢Δ 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> 1 completed 2 3 4 5 1 completed 3 5 2 completed 6 10 ̍͸ح਺ͳͷͰ0,ʂ
  23. Observable͸ඇಉظʹॱʹσʔλ͕ྲྀΕΔ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .map { $0 * 2 }    .subscribe(onNext: { print(“next: \($0)”) }) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ ح਺͚ͩ࢒͢ϑΟϧλΛ͔͚Δ ̎ഒͷ஋ʹม׵͢Δ lOFYUz ਺஋Λϩάग़ྗ͢Δ 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> 1 completed 2 3 4 5 1 completed 3 5 2 completed 6 10 ̍ˎ̎ʹ̎
  24. Observable͸ඇಉظʹॱʹσʔλ͕ྲྀΕΔ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .map { $0 * 2 }    ˠlOFYUz  .subscribe(onNext: { print(“next: \($0)”) }) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ ح਺͚ͩ࢒͢ϑΟϧλΛ͔͚Δ ̎ഒͷ஋ʹม׵͢Δ lOFYUz ਺஋Λϩάग़ྗ͢Δ 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> 1 completed 2 3 4 5 1 completed 3 5 Λग़ྗ͢Δɻ 2 completed 6 10
  25. Observable͸ඇಉظʹॱʹσʔλ͕ྲྀΕΔ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .map { $0 * 2 }     ˠlOFYUz  .subscribe(onNext: { print(“next: \($0)”) }) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ ح਺͚ͩ࢒͢ϑΟϧλΛ͔͚Δ ̎ഒͷ஋ʹม׵͢Δ lOFYUz ਺஋Λϩάग़ྗ͢Δ 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> 1 completed 2 3 4 5 1 completed 3 5 Λग़ྗ͢Δɻ 2 completed 6 10
  26. Observable͸ඇಉظʹॱʹσʔλ͕ྲྀΕΔ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .map { $0 * 2 }     ˠlOFYUz  .subscribe(onNext: { print(“next: \($0)”) }) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ ح਺͚ͩ࢒͢ϑΟϧλΛ͔͚Δ ̎ഒͷ஋ʹม׵͢Δ lOFYUz ਺஋Λϩάग़ྗ͢Δ 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> 1 completed 2 3 4 5 1 completed 3 5 Λग़ྗ͢Δɻ ̎͸ۮ਺ͳͷͰεΩοϓʂ 2 completed 6 10
  27. Observable͸ඇಉظʹॱʹσʔλ͕ྲྀΕΔ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .map { $0 * 2 }      ˠlOFYUz  .subscribe(onNext: { print(“next: \($0)”) }) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ ح਺͚ͩ࢒͢ϑΟϧλΛ͔͚Δ ̎ഒͷ஋ʹม׵͢Δ lOFYUz ਺஋Λϩάग़ྗ͢Δ 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> 1 completed 2 3 4 5 1 completed 3 5 Λग़ྗ͢Δɻ 2 completed 6 10
  28. Observable͸ඇಉظʹॱʹσʔλ͕ྲྀΕΔ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .map { $0 * 2 }       ˠlOFYUz  .subscribe(onNext: { print(“next: \($0)”) }) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ ح਺͚ͩ࢒͢ϑΟϧλΛ͔͚Δ ̎ഒͷ஋ʹม׵͢Δ lOFYUz ਺஋Λϩάग़ྗ͢Δ 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> 1 completed 2 3 4 5 1 completed 3 5 Λग़ྗ͢Δɻ 2 completed 6 10 ̏͸ح਺ͳͷͰ0,ʂ
  29. Observable͸ඇಉظʹॱʹσʔλ͕ྲྀΕΔ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .map { $0 * 2 }        ˠlOFYUz  .subscribe(onNext: { print(“next: \($0)”) }) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ ح਺͚ͩ࢒͢ϑΟϧλΛ͔͚Δ ̎ഒͷ஋ʹม׵͢Δ lOFYUz ਺஋Λϩάग़ྗ͢Δ 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> 1 completed 2 3 4 5 1 completed 3 5 Λग़ྗ͢Δɻ 2 completed 6 10 ̏ˎ̎ʹ̒
  30. Observable͸ඇಉظʹॱʹσʔλ͕ྲྀΕΔ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .map { $0 * 2 }        ˠlOFYUz lOFYUz  .subscribe(onNext: { print(“next: \($0)”) }) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ ح਺͚ͩ࢒͢ϑΟϧλΛ͔͚Δ ̎ഒͷ஋ʹม׵͢Δ lOFYUz ਺஋Λϩάग़ྗ͢Δ 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> 1 completed 2 3 4 5 1 completed 3 5 Λग़ྗ͢Δɻ 2 completed 6 10
  31. Observable͸ඇಉظʹॱʹσʔλ͕ྲྀΕΔ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .map { $0 * 2 }            ˠlOFYUz lOFYUz lOFYUz Λग़ྗ͢Δɻ .subscribe(onNext: { print(“next: \($0)”) }) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ ح਺͚ͩ࢒͢ϑΟϧλΛ͔͚Δ ̎ഒͷ஋ʹม׵͢Δ lOFYUz ਺஋Λϩάग़ྗ͢Δ 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> 1 completed 2 3 4 5 1 completed 3 5 2 completed 6 10 தུ
  32. Observable͸ඇಉظʹॱʹσʔλ͕ྲྀΕΔ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .map { $0 * 2 }            ˠlOFYUz lOFYUz lOFYUz Λग़ྗ͢Δɻ .subscribe(onNext: { print(“next: \($0)”) }) <    > ഑ྻ͔Β0CTFSWBCMFετϦʔϜΛ࡞Δ ح਺͚ͩ࢒͢ϑΟϧλΛ͔͚Δ ̎ഒͷ஋ʹม׵͢Δ lOFYUz ਺஋Λϩάग़ྗ͢Δ 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> 1 completed 2 3 4 5 1 completed 3 5 2 completed 6 10
  33. Observable ͸ subscribeͨ͠Βಈ͖ग़͢ Observable .from([1,2,3,4,5]) .filter { $0 % 2

    == 1 } .map { $0 * 2 } .subscribe(onNext: { print(“next: \($0)”) }) 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> TVCTDSJCF TVCTDSJCF TVCTDSJCF
  34. ಈ͖ग़ͨ͠Β্ྲྀ͔ΒԼྲྀʹΠϕϯτ(σʔλ)͕ྲྀ ΕΔ Observable .from([1,2,3,4,5]) .filter { $0 % 2 ==

    1 } .map { $0 * 2 } .subscribe(onNext: { print(“next: \($0)”) }) 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> TVCTDSJCF TVCTDSJCF TVCTDSJCF PO/FYU PO/FYU PO/FYU
  35. ετϦʔϜͷΠϕϯτ • next
 σʔλ͕ਖ਼͘͠ྲྀΕ͖ͯͨͱ͍͏Πϕϯτɻ೚ҙͷܕͷσʔλΛҾ਺ʹ࣋ ͭɻ • error
 ετϦʔϜத͕Τϥʔ(ྫ֎)Ͱҟৗఀࢭͨ࣌͠ʹྲྀΕΔΠϕϯτɻൃੜͨ͠ ΤϥʔΛҾ਺ʹ࣋ͭɻ •

    completed
 ετϦʔϜ͕ਖ਼͘͠׬ྃͨ࣌͠ʹൃੜ͢ΔΠϕϯτɻετϦʔϜͷ࠷ޙʹ Ұ౓͚ͩൃੜ͢Δɻ
 ʢೖྗΛ଴ͪଓ͚͍ͨͱ͖͸completedΛྲྀ͞ͳ͍ʣ
  36. ετϦʔϜͷΠϕϯτ next completed next next next next next completed next

    next ετϦʔϜ͸ next ͰσʔλΛྲྀ͠ɺDPNQMFUFEͰऴྃ͢Δ DPNQMFUFE͕དྷͨΒɺͦͷઌ͸OFYUFSSPS͸ྲྀΕͳ͍ ྲྀͤͳ͍ next next next FSSPS ྫ֎ ͕དྷͨΒɺͦͷઌ͸OFYUDPNQMFUFEFSSPS͸ྲྀΕͳ͍ ྲྀͤͳ͍ error next next next next next DPNQMFUFE͠ͳ͍ετϦʔϜͰӬٱʹOFYUΛ଴ͪଓ͚Δ͜ͱ΋Ͱ͖Δ next
  37. Ϣʔβ͔ΒͷϘλϯೖྗΠϕϯτΛ ଴ͪଓ͚Δ0CTFSWBCMF 0CTFSWBCMF*OU Disposableͷ࢖͍ํ %JTQPTBCMF TVCTDSJCF͸ %JTQPTBCMFΛฦ͢ let observable: Observable<Void>

    = executeButton.rx.tap .asObservable() EJTQPTFͰ TVCTDSJCFΛఀࢭ let disposable = observable.subscribe(onNext: { print(“Ϙλϯ͕ԡ͞Ε·ͨ͠”) }) ্ͷ0CTFSWBCMFΛ TVCTDSJCFͰ଴ͪଓ͚ΔॲཧΛ։࢝
  38. ্ྲྀͷεϨουΛม͑Δ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .subscribe(onNext: { print(“next: \($0)”) }) 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> TVCTDSJCF TVCTDSJCF PO/FYU PO/FYU TVCTDSJCF࣮ߦεϨου ϝΠϯεϨου όοΫάϥ΢ϯυεϨου
  39. ্ྲྀͷεϨουΛม͑Δ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .subscribe(onNext: { print(“next: \($0)”) }) 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> TVCTDSJCF TVCTDSJCF TVCTDSJCF PO/FYU PO/FYU PO/FYU .subscribeOn(ConcurrentD ispatchQueueScheduler(qos : .background)) TVCTDSJCF࣮ߦεϨου ϝΠϯεϨου όοΫάϥ΢ϯυεϨου
  40. ԼྲྀͷεϨουΛม͑Δ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .subscribe(onNext: { print(“next: \($0)”) }) 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> TVCTDSJCF TVCTDSJCF PO/FYU PO/FYU TVCTDSJCF࣮ߦεϨου ϝΠϯεϨου όοΫάϥ΢ϯυεϨου
  41. ԼྲྀͷεϨουΛม͑Δ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .subscribe(onNext: { print(“next: \($0)”) }) 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> TVCTDSJCF TVCTDSJCF TVCTDSJCF PO/FYU PO/FYU PO/FYU .observeOn(MainSchedula r.instance) TVCTDSJCF࣮ߦεϨου ϝΠϯεϨου όοΫάϥ΢ϯυεϨου
  42. ૊Έ߹Θͤ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .subscribe(onNext: { print(“next: \($0)”) }) 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> TVCTDSJCF TVCTDSJCF TVCTDSJCF PO/FYU PO/FYU PO/FYU .subscribeOn(ConcurrentD ispatchQueueScheduler(qos : .background)) .observeOn(MainSchedula r.instance) PO/FYU TVCTDSJCF TVCTDSJCF࣮ߦεϨου ϝΠϯεϨου όοΫάϥ΢ϯυεϨου 0CTFSWBCMF*OU
  43. Observable .zip( ) ฒྻͭͳ͗(zip ΦϖϨʔλ) Observable .from([1,2,3,4,5]) 0CTFSWBCMF*OU 0CTFSWBCMF*OU Observable

    .from([10,9,8,7,6]) 0CTFSWBCMF*OU 0CTFSWBCMF *OU *OU                     .map { a, b in a * b }
  44. Observable .zip( ) ฒྻͭͳ͗(zip ΦϖϨʔλ) Observable .from([1,2,3,4,5]) 0CTFSWBCMF*OU 0CTFSWBCMF*OU Observable

    .from([10,9,8,7,6]) 0CTFSWBCMF*OU 0CTFSWBCMF *OU *OU                     .map { a, b in a * b } [JQΦϖϨʔλͰ " #݁߹ ී௨ͷ0CTFSWBCMF" ී௨ͷ0CTFSWBCMF# ݁߹݁Ռ͸ " #  " #  ͱ͍͏ετϦʔϜ
  45. .flatMap{ a in } self.fetchAPI(a) ௚ྻͭͳ͗(flatMap ΦϖϨʔλ) Observable .from([1,2,3,4,5]) B*OU

    0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU .map { $0 * 2 }
  46. .flatMap{ a in } self.fetchAPI(a) ௚ྻͭͳ͗(flatMap ΦϖϨʔλ) Observable .from([1,2,3,4,5]) B*OU

    0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU qBU.BQΦϖϨʔ λͰ "͔Β#ʹͭͳ͙ ී௨ͷ0CTFSWBCMF" "͔ΒྲྀΕͨ஋Λ࢖͍ɺ "1*௨৴ͯ݁͠ՌΛྲྀ͢ 0CTFSWBCMF# .map { $0 * 2 }
  47. ෼ׂ΋Ͱ͖Δ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .map { $0 * 2 } .subscribe(onNext: { print(“next: \($0)”) }) 0CTFSWBCMF*OU 0CTFSWBCMF*OU 0CTFSWBCMF*OU <*OU> %JTQPTBCMF
  48. ෼ׂ΋Ͱ͖·͢ Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1

    } .subscribe(onNext: { print(“next: \($0)”) }) 0CTFSWBCMF*OU <*OU> .map { $0 * 2 } .subscribe(onNext: { print(“next: \($0)”) }) 0CTFSWBCMF*OU 0CTFSWBCMF*OU .subscribe(onNext: { print(“next: \($0)”) }) %JTQPTBCMF %JTQPTBCMF %JTQPTBCMF
  49. ෼ׂͨ͠৔߹͸subscribe࣌ʹετϦʔϜม׵աఔ ͕ෳ੡͞Ε·͢ɻ Observable .from([1,2,3,4,5]) .filter { $0 % 2 ==

    1 } .subscribe(onNext: { print(“next: \($0)”) }) 0CTFSWBCMF*OU <*OU> .map { $0 * 2 } 0CTFSWBCMF*OU 0CTFSWBCMF*OU .subscribe(onNext: { print(“next: \($0)”) }) Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1 } 0CTFSWBCMF*OU <*OU> 0CTFSWBCMF*OU .map { $0 * 2 } .subscribe(onNext: { print(“next: \($0)”) }) 0CTFSWBCMF*OU Observable .from([1,2,3,4,5]) .filter { $0 % 2 == 1 } 0CTFSWBCMF*OU <*OU> 0CTFSWBCMF*OU %JTQPTBCMF %JTQPTBCMF %JTQPTBCMF ͢΂ͯผͷաఔͰ ετϦʔϜΛ TVCTDSJCF͍ͯ͠Δ
  50. (தڃ)ෳ੡ͤ͞ͳ͍ͨΊͷtips Hot ͱ Cold Observable ͸ Cold / Hot ʹ෼͔Ε͍ͯͯɺ

    جຊ͸໌ࣔతʹHotม׵͠ͳ͍ݶΓ Cold Observable ͱࢥ͓͍ͬͯ ͨ΄͏͕͍͍Ͱ͢ɻ(ݫີʹ͸ҧ͍·͕͢؆୯ͷͨΊ) Cold͸subscribe͞ΕΔ·Ͱಈ͔ͣɺsubscribeͷ౎౓ෳ੡͞ΕΔ(1͔ ΒετϦʔϜ͕࢝·Δ)ͱ͍͏ಛ௃͕͋Γ·͢ɻ Hot͸subscribe͞Εͳͯ͘΋ಈ͖ɺ̎ͭҎ্ͷsubscribe͕͋ͬͯ΋ ಉ͡ετϦʔϜΛڞ༗͠·͢ɻ
  51. Hot / Cold ม׵: Cold → Hot share(), shareReplay(), publish()

    ͳͲͷ Hotม׵ΦϖϨʔλʹΑΓHot ʹม׵Ͱ͖· ͢ɻ
  52. Hot / Cold ม׵: Hot → Cold share(), shareReplay(), publish()

    Ҏ֎ͷ΄΅ ͢΂ͯͷΦϖϨʔλ͸ Cold ͳ Observable Λฦ͠·͢ɻ ྫɿ filter, map, from, flatMap, zipͳͲ
  53. Hotม׵ͱݺ͹ΕΔΦϖϨʔλʹΑΓɺ ෳ੡͞Εͳ͘ͳΓ·͢ɻ Observable .from([1,2,3,4,5]) .filter { $0 % 2 ==

    1 } 0CTFSWBCMF*OU <*OU> 0CTFSWBCMF*OU %JTQPTBCMF .share() %JTQPTBCMF IPUม׵ͷྫ TIBSF ΦϖϨʔλ 0CTFSWBCMF*OU .subscribe(onNext: { print(“next: \($0)”) }) .subscribe(onNext: { print(“next: \($0)”) }) ಉ͡ετϦʔϜΛ࢖͍ճ͢ ʹ৽ͨʹੜ੒͠ͳ͍͍ͯ͘෼ίετ͕௿͍
  54. Rx ͸؆୯ʹࢼͤ·͢ • Java, Kotlin ։ൃ؀ڥ͕͋ΔͳΒ… • Grade Ͱ RxJava2,

    RxAndroid, RxKotlinΛೖΕͯΈΔɻ • Swift ։ൃ؀ڥ͕͋ΔͳΒ… • CocoaPods Ͱ RxSwiftΛೖΕͯΈΔɻ • Ͳͬͪ΋ͳ͍৔߹ • RxJS Playground ͰͳΜͱϒϥ΢βͰࢼͤΔʂ • https://rxjs-playground.github.io/ (RxJS Playground Ͱݕࡧ)
  55. ͍͞͝ʹ • tech vein Ͱ͸͜ΜͳΞϓϦΤϯδχΞɺ
 ϑϦʔϥϯεɾύʔτφʔ༷Λืू͍ͯ͠·͢ɻ • RxSwift, RxJava Λ࢖͍͍ͨ

    • MVVMɺ CleanArchitectureɺ DI Λ׆༻ͨ͠ઃܭʹڵຯ͕͋Δ ڵຯ͕͋Δํɺͪΐͬͱ࿩Λฉ͍ͯΈ͍ͨํ
 ͨͩ3Yͷ࿩Λ΋ͬͱฉ͖͍ͨํ΋
 ͥͻ͓੠͕͚Լ͍͞ʂ
  56. ࢀߟ • RxSwift / RxJava Observable νʔτγʔτ
 https://docs.google.com/spreadsheets/u/1/d/ 12C3MLr2RfrYPezHJTmjruDt3i8TRZGCJDnVz86vo39g/edit#gid=0 •

    SubjectͱRelayͷҧ͍ͳͲObservableͷ೿ੜΫϥεͷ࢓༷Λ·ͱΊͨਤͰ͢ɻ • RxSwift + RxCocoa Λ࢖ͬͨ؆୯ͳΞϓϦ • GitHub:
 https://github.com/ecoopnet/rxswift-beginner • ࢀߟهࣄ • Կͱͳ͘RxJavaΛ࢖ͬͯΔ͚Ͳਖ਼௚Α͘෼͔ͬͯͳ͍ਓ͕ಡΉͱྑͦ͞͏ͳهࣄɾجૅฤ
 https://qiita.com/k-mats/items/4d374460a3f6284dd09f