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

はじめてのRxSwift体験記

Adbff2a1447fbe53ca24a6943b3d80e4?s=47 haptaro
November 17, 2016

 はじめてのRxSwift体験記

第3回RxSwift勉強会@Sansanの発表資料になります

Adbff2a1447fbe53ca24a6943b3d80e4?s=128

haptaro

November 17, 2016
Tweet

Transcript

  1. ͸͡ΊͯͷRxSwiftମݧه @haptaro

  2. • ෱ඌ ޾ଠ࿠ • Sansanגࣜձࣾ Eightࣄۀ෦ • iOSΞϓϦΤϯδχΞ (ΞϧόΠτ)

  3. RxSwift

  4. ΞδΣϯμ • ཧղ͕೉͔ͬͨ͠ͱ͜Ζ4બ • ·ͱΊ

  5. ཧղ͕೉͔ͬͨ͠ͱ͜Ζ4બ • DisposeBag • shareReplay • combineLatest • flatMapLatest

  6. DisposeBag

  7. ຖճετϦʔϜͷ࠷ޙʹ͍͍ͭͯΔ
 ۚڕͷฅΈ͍ͨͳ͍ͭ͜…

  8. disposeBag DisposableΛೖΕ͓ͯ͘ାͷΑ͏ͳ΋ͷ RxSwiftͰͷϝϞϦ؅ཧ ※subscribe΋bindTo΋DisposableΛฦ͢

  9. disposeBagΛϓϩύςΟʹ͍࣋ͬͯΔΦϒδΣΫτͷ deallocͷλΠϛϯάͰାͷத਎Λഁغͯ͘͠ΕΔ ※ݫີʹ͸ାʹೖΕͨDisposable֤ʑͰdispose()͕ݺ͹ΕΔ

  10. shareReplay

  11. ·ͨۚڕͷฅΈ͍ͨʹ͍ͭͯΔ…

  12. Hot / Cold • Cold Observableɾɾɾsubscribe͢Δ·Ͱ࣮ߦ ͞Εͳ͍ɻ΄ͱΜͲͷΦϖϨʔλ͸Cold • Hot Observableɾɾɾ෼ذͰ͖ɺు͍ͨΠϕϯ

    τ͸֤ObserverͰڞ༗͞ΕΔ
  13. Cold Observable͸෼ذͰ͖ͳ͍ Cold Cold Observer Observer Cold Cold Cold Cold

  14. Hot Observable͸෼ذͰ͖Δ Cold Cold Hot Observer Observer

  15. Observer Observable

  16. shareReplay Hotม׵ΦϖϨʔλ

  17. Observer Observer Observer Observer Observer Observable

  18. combineLatest

  19. 2ͭͷObservableΛҾ਺ͱͯ͠ड͚औ͍ͬͯΔ͍ͭ͜… ͦͯ͠LatestͬͯͳΜ΍…

  20. Rxʹ͓͚ΔʮLatestʯ͸ Observable͔Βు͔Εͨ
 ʮ࠷৽ʯͷ஋ ͍ͭ͜ʂ ࠷৽

  21. ෳ਺ͷObservable͕ు͍ͨ࠷ ৽ͷ஋Λͭͳ͍͛ͨͱ͖ʹ࢖͏

  22. flatMapLatest

  23. Ϋϩʔδϟ಺ͰObservableΛฦ͍ͯ͠Δͷ͔…ʁ ͜Ε͸ObservableͱObservableΛͭͳ͍͛ͯΔͷ͔…ʁ

  24. flatMapΛཧղ͢Δʹ͸·ͣ͸

  25. map

  26. 1 2 3 4 2 4 6 8 .map {

    $0 * 2 }
  27. flatMap

  28. mapػೳʹՃ͑ͯɺObservableΛണ͕ ͯ͘͠ΕΔ(ۉͯ͘͠ΕΔ)Α͏ͳ΋ͷ

  29. flatMap = map + flatten ※flatMapLatest = map + switchLatest

  30. getText() -> Observable<String> use(text: String) -> Observable<Void> 2ͭͷؔ਺Λߟ͑Δ…

  31. getTextؔ਺ͰಘͨจࣈྻΛ useؔ਺Ͱ࢖͍͍ͨ…

  32. mapͩͱ Observable<String>

  33. mapͩͱ Observable<String> Observable<Observable<String>>

  34. flatMapͩͱ Observable<String>

  35. flatMapͩͱ Observable<String> Observable<Observable<String>>

  36. flatMapͩͱ Observable<String> Observable<Observable<String>> Observable<String>

  37. getText().flatMap { text return use(text) }

  38. ͜͜Ͱ๻͸ٙ໰ʹ…

  39. flatMapLatest͸ͳΜ͔Θ͔͚ͬͨͲɺ combineLatest΋ObservableΛͭͳ͛Δͱ͔ͳΜ ͱ͔… ࢖͍෼͚ͱ͔ϙΠϯτ஌Γ͍ͨ…

  40. ϙΠϯτ • combineLatestɾɾɾෳ਺ͷObservable͕ు͍ͨ ࠷৽ͷ஋Λͭͳ͍͛ͨͱ͖ • flatMapLatestɾɾɾʮObservableΛฦ͢ॲཧʯ ͷʮ݁ՌʯΛʮॱংʯΛؾʹͯͭ͠ͳ͍͛ͨͱ ͖

  41. ·ͱΊ • ࠷ॳ͸ͬ͞ͺΓ͕ͩͬͨঃʑʹݟ͑ͯ͘ΔͷͰ׳Εͷ෦෼͕େ͖ ͍ • ObservableͱObserver͕Կ͔Λߟ͑ɺΦϖϨʔλͰՃ޻͍ͯ͠ ͘ࡍ͸ܕΛҙࣝ͢Δ͜ͱ͕ॏཁ • ֓೦ͷཧղ΍εϨουͷ؅ཧͳͲΛ͔ͬ͠Γ͍ͯ͠ͳ͍ͱύϑΥʔ Ϛϯε͕Լ͕ͬͨΓɺҙਤ͠ͳ͍ಈ͖Λ͢Δةݥੑ΋͋Δ

    • RxSwiftͷجૅతͳ෦෼͸ཧղͭͭ͋͠ΔͷͰɺࠓޙ͸࣮ࡍʹϓ ϩμΫτͰ࢖͍ͳ͕ΒֶΜͰ͍͖͍ͨ
  42. ཧղͷ໾ཱͬͨࢿྉ • RxSwift͸։ൃΛͲ͏ม͔͑ͨ
 https://speakerdeck.com/ishkawa/rxswifthakai-fa-wodoubian-etaka • RxExample͔ΒֶͿʂRxSwift
 https://speakerdeck.com/kazu0620/rxexamplekaraxue-bu-rxswift • ࠓ೔ͦ͜ཧղ͢Δ Hot

    / Cold 
 http://www.slideshare.net/yukitakahashi3139241/hot-cold • shareReplayΛͪΌΜͱॻ͍͓ͯߦّྑ͘ετϦʔϜΛߪಡ͠Α͏
 http://qiita.com/kazu0620/items/bde4a65e82a10bd33f88 • RxSwiftͷobserveOnͱsubscribeOnΛཧղ͢Δ
 https://speakerdeck.com/bricklife/rxswiftfalseobserveontosubscribeonwoli-jie-suru

  43. Happy RxSwift Life!