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

いまさら聞けないRxSwift入門

 いまさら聞けないRxSwift入門

モバイルメソッド大阪第一回 RxSwift入門資料です。

M.Inomata

May 10, 2018
Tweet

More Decks by M.Inomata

Other Decks in Programming

Transcript

  1. ͍·͞Βฉ͚ͳ͍
    RxSwiftೖ໳
    גࣜձࣾ tech vein
    ழມ ॆԝ

    View full-size slide

  2. ࣗݾ঺հ
    גࣜձࣾ tech vein ୅ද
    ழມ ॆԝ (͍ͷ·ͨ ΈͭͻΖ)
    ձࣾHP: https://www.tech-vein.com/
    private twitter: @ino2222

    View full-size slide

  3. ެ։தͷAndroid/iOSΞϓϦ
    ι΢ϧΞϯϦʔγϡ
    ࣗ෼ͷΦϦΩϟϥΛ࡞ΔεϚϗ31(
    IUUQTPVOUFDIWFJODPN IUUQTCJUMZ);BV3
    $IJJ2
    ஍ҬͰॿ͚߹͑Δಗ໊૬ஊΞϓϦ
    IUUQTBQQMFDP,[IYD
    ϙέοτΞϒετϥΫτ
    ҩྍैࣄऀ޲͚ͷ࠷৽δϟʔφϧӾཡΞϓϦ

    View full-size slide

  4. ࠓ೔͓࿩͢Δ͜ͱ
    • RxSwiftͱ͸
    • RxSwiftͷجຊ
    • ͜Ε͚֮ͩ͑ͨΒ࢖͑ΔΑ͏ʹͳΔ͔΋ʁ

    View full-size slide

  5. ࠓ೔͓࿩͠ͳ͍͜ͱ(஌ͬͯΔਓ޲͚)
    • Cold -> Hot ม׵
    • Reactive Streams(backPressure ͳͲ)

    View full-size slide

  6. RxSwiftͱ͸
    • ֤ݴޠͰ࣮૷͞Ε͍ͯΔʮRx(Reactive Extension)ʯ

    ͷSwift޲͚ϥΠϒϥϦ
    • Reactive Extension … ΋ͱ΋ͱ͸

    C#.NET Ͱ࢖ΘΕͩͨ͠ Rx.NET ͕ΦϦδφϧ
    • Java(RxJava), JavaScript(RxJS), C++(RxCpp),
    Ruby(Rx.rb), PHP(RxPHP) ͳͲͳͲଟ਺

    View full-size slide

  7. ReactiveXͱ͸Կ͔ʁ
    ReactiveX = Reactive Extension
    Reactive Programming ʹ൓ԠతϓϩάϥϛϯάΛ͢Δͨ
    Ίͷ
    Extension = ֦ு
    ReactiveX͸
    - σβΠϯύλʔϯͷ Observable ύλʔϯ
    - σβΠϯύλʔϯͷ Iterator ύλʔϯ
    - ؔ਺ܕϓϩάϥϛϯά
    Λ૊Έ߹Θͤͯ࡞ΒΕͨϥΠϒϥϦͷ͜ͱ

    View full-size slide

  8. Observerύλʔϯ
    σʔλΛ؂ࢹ͢Δਓͱ఻͑Δ࢓૊ΈΛ
    ந৅Խͨ͠σβΠϯύλʔϯ
    ͕͜͜େࣄʂ

    View full-size slide

  9. Iterator ύλʔϯ
    https://ja.wikipedia.org/wiki/Iterator_ύλʔϯ
    Ұ࿈ͷσʔλΛॱ൪ʹॲཧ͢ΔσβΠϯύλʔϯ
    ͕͜͜େࣄʂ

    View full-size slide

  10. ؔ਺ܕϓϩάϥϛϯά
    ˠ
    ͦΕͧΕͷؔ਺͸
    */065ʹ͔͠
    Өڹ͠ͳ͍ ʹ෭࡞༻͕ͳ͍

    View full-size slide

  11. ͦͯ͠Rx΁…

    View full-size slide

  12. ͕͜͜خ͍͠RxSwift
    ࣌ܥྻσʔλΛؚΉɺ͋ΒΏΔσʔλΛετϦʔϜͱͯ͠εϚʔτʹॲཧͰ͖Δɻ
    ετϦʔϜͱͯ͠ॱ൪ʹॲཧͰ͖Δ͜ͱͰɺ΍΍͍͜͠ඇಉظ੍ޚ͕γϯϓϧʹͳΔɻ
    →ίʔϧόοΫ஍ࠈ͔Βͷղ์
    ந৅ԽʹΑΓૹΓखɾड͚औΓखΛ௚઀஌Βͳ͍··ॲཧ͕ग़དྷΔͷͰૄ݁߹ʹͳΔɻ
    →վมͷӨڹൣғ͕খ͘͞ͳΔ
    →෦඼Խɾڞ௨Խ͕ਐΉ
    ֤छετϦʔϜΛѻ͏ศརͳಓ۩͕ἧ͍ͬͯΔɻ
    →ετϦʔϜͷੜ੒ɾม׵ɾ߹੒Λߦ͏ΦϖϨʔλ܈
    →ετϦʔϜͷεϨου੾Γସ͑ػೳ

    View full-size slide

  13. RxSwiftͷجຊ

    View full-size slide

  14. RxSwiftͷجຊ
    ͜Ε͚ͩ஌ͬͯͨΒ
    RxŧŔŕŪɹɹ ũƄŝſͬͯݴ͑Δ

    View full-size slide

  15. RxSwiftͷجຊ
    ͜Ε͚ͩ஌ͬͯͨΒ
    RxŧŔŕŪ(ŦƄş)ũƄŝſͬͯݴ͑Δ͔΋ʁ

    View full-size slide

  16. Observable, Subject,
    Disposable
    ΩϞʹͳΔͷ͸ɺ
    ͜ͷ̏ܥ౷ͷΫϥε(ϓϩτίϧ)ɻ
    Ϋϥε͸৭ʑ͋Γ·͕͢ɺ
    ͍͍ͩͨ͜ΕΒͷ஥ؒͰ͢ɻ

    View full-size slide

  17. + subscribe()
    Observable
    + onNext()
    + onError()
    + onCompleted()
    + subscribe()
    Subject
    ܧঝ
    + dispose()
    Disposable
    subscribe() ͷ໭Γ஋
    Observable, Subject,
    Disposable

    View full-size slide

  18. Observable
    ௚༁͢Δͱɺ؂ࢹ͢Δ͜ͱ͕Ͱ͖Δର৅ɻ
    ೚ҙͷܕ T ʹ͍ͭͯɺ؂ࢹͯ͠σʔλʢΠϕϯτ)Λड͚औ
    Δࣄ͕Ͱ͖ΔΫϥεɾΠϯελϯεͰ͢ɻ
    ࣌ܥྻͰσʔλ͕ObservableʹྲྀΕΔ͜ͱ͔Βɺ
    Observable͸ετϦʔϜ(Stream)ͱ΋ݺͼ·͢ɻ
    + subscribe()
    Observable
    + onNext()
    + onError()
    + onCompleted()
    + subscribe()
    Subject
    ܧঝ
    + dispose()
    Disposable
    subscribe() ͷ໭Γ஋

    View full-size slide

  19. “Hello” “Rx” “World”
    Observable
    completed
    ࣌ؒͷྲྀΕ
    ࣮ߦ݁ՌɿԼͷॱʹग़ྗ͞ΕΔɻ
    OFYU )FMMP
    OFYU 3Y
    OFYU 8PSME
    DPNQMFUFE

    View full-size slide

  20. Subject
    ؆୯ʹݴ͏ͱɺ೚ҙͷσʔλΛྲྀ͢ػೳ(Observer
    ͱͯ͠ͷػೳ)Λ࣋ͬͨObservable࣮૷ɻ
    RxSwift ͷSubject͸
    PublishSubject, BehaviorSubject, ReplaySubject
    ͷ̏छྨɻ
    + subscribe()
    Observable
    + onNext()
    + onError()
    + onCompleted()
    + subscribe()
    Subject
    ܧঝ
    + dispose()
    Disposable
    subscribe() ͷ໭Γ஋

    View full-size slide

  21. Subject͸ObserverΛܧঝ͍ͯ͠ΔͷͰ
    Observableͱͯ͠ѻ͑Δɻ
    Subject͸onNext(), onComplete(), onError()
    ͳͲͷσʔλɾΠϕϯτൃ৴ϝιουΛ࣋ͭɻ

    View full-size slide

  22. ετϦʔϜͷΠϕϯτ
    • next

    σʔλ͕ਖ਼͘͠ྲྀΕ͖ͯͨͱ͍͏Πϕϯτɻ೚ҙͷܕͷσʔλΛҾ਺ʹ࣋
    ͭɻ
    • error

    ετϦʔϜத͕Τϥʔ(ྫ֎)Ͱҟৗఀࢭͨ࣌͠ʹྲྀΕΔΠϕϯτɻൃੜͨ͠
    ΤϥʔΛҾ਺ʹ࣋ͭɻ
    • completed

    ετϦʔϜ͕ਖ਼͘͠׬ྃͨ࣌͠ʹൃੜ͢ΔΠϕϯτɻετϦʔϜͷ࠷ޙʹ
    Ұ౓͚ͩൃੜ͢Δɻ

    ʢೖྗΛ଴ͪଓ͚͍ͨͱ͖͸completedΛྲྀ͞ͳ͍ʣ

    View full-size slide

  23. ετϦʔϜͷΠϕϯτ
    next completed
    next next next next
    next completed
    next next
    ετϦʔϜ͸ next ͰσʔλΛྲྀ͠ɺDPNQMFUFEͰऴྃ͢Δ
    DPNQMFUFE͕དྷͨΒɺͦͷઌ͸OFYUFSSPS͸ྲྀͤͳ͍
    next next next
    FSSPS ྫ֎
    ͕དྷͨΒɺͦͷઌ͸OFYUDPNQMFUFEFSSPS͸ྲྀͤͳ͍
    error

    View full-size slide

  24. RxSwift ͷ Subject࢖͍෼͚
    ɾPublishSubject
    ɹաڈͷσʔλΛอ࣋͠ͳ͍Subjectɻ
    ɹར༻ྫɿϘλϯͷλοϓΠϕϯτ
    ɾBehaviorSubject
    ɹաڈͷσʔλͷ͏ͪ௚ۙͷ̍ͭΛอ͍࣋ͯ͠ΔSubjectɻ
    ɹར༻ྫɿεΠονON/OFFঢ়ଶɾϢʔβεςʔλεͳͲ
    ɾReplaySubject.create(bufferSize: Int)
    ɹաڈͷ௚ۙσʔλΛࢦఆόοϑΝʹೖΔ͚ͩอ͍࣋ͯ͠ΔSubjectɻ
    ɹόοϑΝαΠζ̌ͳΒPublishSubjectͱಉ͡ɻ
    ɹόοϑΝαΠζ̍ͳΒBehaviorSubjectͱಉ͡ɻ
    ɹར༻ྫɿࢥ͍͔ͭͳ͍…ཤྺܥͰ࢖͑Δ͔΋ʁ

    View full-size slide

  25. PublishSubject
    PublishSubject
    completed
    ࣌ؒͷྲྀΕ
    TVCTDSJCF

    TVCTDSJCF

    View full-size slide

  26. BehaviorSubject
    BehaviorSubject
    completed
    ࣌ؒͷྲྀΕ
    TVCTDSJCF

    TVCTDSJCF

    TVCTDSJCF

    View full-size slide

  27. ReplaySubject
    ReplaySubject.create(bufferSize: 2)
    completed
    ࣌ؒͷྲྀΕ
    TVCTDSJCF

    TVCTDSJCF

    TVCTDSJCF

    View full-size slide

  28. Disposable
    ௚༁͢Δͱɺഇغɾॲ෼Ͱ͖Δ΋ͷɻ
    ObservableΛ subscribe(ߪಡ) ͢Δͱ໭Γ஋ͱͯ͠ Disposable Λ
    ฦ͠·͢ɻ
    Disposableʹରͯ͠dispose()ΛݺͿ͜ͱͰɺඇಉظॲཧͷΩϟϯη
    ϧͳͲɺ؂ࢹ͢Δଆͱͯ͠ॲཧΛࢭΊΔࣄ͕Ͱ͖·͢ɻ
    ྫɿը໘Λดͨ͡Β௨৴Ωϟϯηϧͨ͠Γɺೖྗ଴ͪΛࢭΊͨΓ͢
    Δɻ
    + subscribe()
    Observable
    + onNext()
    + onError()
    + onCompleted()
    + subscribe()
    Subject
    ܧঝ
    + dispose()
    Disposable
    subscribe() ͷ໭Γ஋

    View full-size slide

  29. + subscribe()
    Observable
    + onNext()
    + onError()
    + onCompleted()
    + subscribe()
    Subject
    ܧঝ
    + dispose()
    Disposable
    subscribe() ͷ໭Γ஋
    ॲཧͷྲྀΕ

    View full-size slide

  30. + subscribe()
    Observable
    + onNext()
    + onError()
    + onCompleted()
    + subscribe()
    Subject
    ܧঝ
    + dispose()
    Disposable
    subscribe() ͷ໭Γ஋
    subscribe(onNext: … )
    ॲཧͷྲྀΕ

    View full-size slide

  31. + subscribe()
    Observable
    + onNext()
    + onError()
    + onCompleted()
    + subscribe()
    Subject
    ܧঝ
    + dispose()
    Disposable
    subscribe() ͷ໭Γ஋
    subscribe(onNext: … )
    ΠϕϯτΛҾ਺ͷΫϩʔδϟͰ଴ͪड͚Δ
    PO/FYU lIFMMPz

    PO/FYU l3Yz

    PO/FYU l8PSMEz

    ʜ
    PO$PNQMFUFE

    ॲཧͷྲྀΕ

    View full-size slide

  32. + subscribe()
    Observable
    + onNext()
    + onError()
    + onCompleted()
    + subscribe()
    Subject
    ܧঝ
    + dispose()
    Disposable
    subscribe() ͷ໭Γ஋
    subscribe(onNext: … )
    dispose()
    ΠϕϯτΛҾ਺ͷΫϩʔδϟͰ଴ͪड͚Δ
    ॲཧͷྲྀΕ

    View full-size slide

  33. + subscribe()
    Observable
    + onNext()
    + onError()
    + onCompleted()
    + subscribe()
    Subject
    ܧঝ
    + dispose()
    Disposable
    subscribe() ͷ໭Γ஋
    subscribe(onNext: … )
    dispose()
    ΠϕϯτΛҾ਺ͷΫϩʔδϟͰ଴ͪड͚Δ
    ॲཧΛࢭΊͨ͘ͳͬͨΒEJTQPTFΛݺͿ
    ॲཧͷྲྀΕ

    View full-size slide

  34. (x, y) (x2, y2) (x3, y3)
    ྫɿλονΠϕϯτͷ؂ࢹ Observable)
    ࣌ؒͷྲྀΕ
    (x4, y4)
    TVCTDSJCF

    EJTQPTF

    EJTQPTFE
    (x2, y2) (x3, y3)
    Disposable.dispose()

    View full-size slide

  35. DisposeBag
    disposeΛ͍͍ͪͪ͢Δͷ͸ΊΜͲ͍͘͞ɻ
    ͦΜͳͱ͖͸DisposeBagɻ

    View full-size slide

  36. DisposeBag
    ը໘ͳͲɺ؂ࢹݩΠϯελϯε͕ࢮΜͩΒࣗಈͰͥΜ
    Ϳdisposeͯ͠΄͍࣌͠ʹ࢖͑ΔศརΫϥεɻ
    DisposeBagΠϯελϯεʹಥͬࠐΜͰ͓͘ͱɺ
    disposeBag͕ղ์͞ΕΔ࣌ʹ·ͱΊͯdisposeBagͷ
    தͷdisposableΛdisposeͯ͘͠ΕΔɻ
    ˠ3Yඪ४Ͱ͸ͳ͍Ͱ͕͢ΊͪΌͪ͘Ό࢖͍·͢ɻ
    ɹͱΓ͋͑ͣ%JTQPTF#BH࡞ͬͯ์ΓࠐΜͰ͓͚͹͍͍ͱࢥ͏ɻ

    View full-size slide

  37. (x, y) (x2, y2) (x3, y3)
    ྫɿλονΠϕϯτͷ؂ࢹ Observable)
    ࣌ؒͷྲྀΕ
    (x4, y4)
    TVCTDSJCF

    EJTQPTF

    EJTQPTFE
    (x2, y2) (x3, y3)
    EJTQPTFE CZEJTQPTF#BH

    Subscibe ॲཧ
    DisposeBag
    JOJU
    EFJOJU

    View
    %JTQPTBCMFΛ
    %JTQPTF#BHͷ
    தʹೖΕΔɻ
    %JTQPTF#BH͸
    ഁغ͞ΕΔͱ
    தͷ%JTQPTBCMFΛ
    EJTQPTF͢Δɻ

    View full-size slide

  38. IUUQTHJUIVCDPNFDPPQOFUSYTXJGUCFHJOOFSCMPCNBTUFS3Y4XJGU4BNQMF3Y4XJGU4BNQMF
    6*4BNQMF7JFX$POUSPMMFSTXJGU
    DisposeBagͷར༻ྫ
    ˡ6*7JFX$POUSPMMFSͷ
    ϑΟʔϧυʹEJTQPTF#BHΛஔ͍͓ͯ͘

    ˠը໘͕ดͨ͡Β6*7JFX$POUSPMMFS͕ࢮ͵ɻ
    ˠ6*7JFX$POUSPMMFS͕ࢮΜͩΒ%JTQPTF#BH΋ࢮ͵ɻ
    ˠ%JTQPTF#BH͕ࢮΜͩΒඥͮ͘EJTQPTBCMF͕
    ɹEJTQPTF
    ͞ΕΔɻ

    View full-size slide

  39. ΦϖϨʔλ(Operator)
    Rxʹ͸ Observable, SubjectͰ࡞ͬͨετϦʔϜΛૢ
    ࡞͢Δػೳ͕ఏڙ͞Ε͍ͯ·͢ɻ
    ετϦʔϜ(=Observable)Λม׵ɾ߹੒ɾੜ੒͢Δૢ
    ࡞ͷ͜ͱΛʮΦϖϨʔλʯͱݺͼ·͢ɻ

    View full-size slide

  40. ετϦʔϜΛ࡞Δ
    ̍ … Subject Ϋϥε͔Β࡞Δ
    ̎ … from(), of(), just() ͳͲͰ೚ҙͷ഑ྻɾ஋Λ
    ม׵ͯ͠࡞Δ
    ̏ … Observable.create() Ͱ೚ҙͷॲཧ͔Β࡞Δ

    View full-size slide

  41. PublishSubject
    completed
    ࣌ؒͷྲྀΕ
    TVCTDSJCF

    TVCTDSJCF

    View full-size slide

  42. 10
    Observable.just(10)
    completed
    σʔλετϦʔϜͷྲྀΕ
    just( )
    ετϦʔϜม׵

    View full-size slide

  43. “a”
    Observable.of(“a”, ”b”, ’c”, ”d”, “e”)
    completed
    σʔλετϦʔϜͷྲྀΕ
    of( , , , , )
    ετϦʔϜม׵
    “b” “c” “d” “e”

    View full-size slide

  44. 1
    Observable.from([1, 3, 5, 7, 9])
    completed
    σʔλετϦʔϜͷྲྀΕ
    from([ , , , , ])
    ετϦʔϜม׵
    3 5 7 9

    View full-size slide

  45. Observable.error(MyError())
    σʔλετϦʔϜͷྲྀΕ
    error( )
    ετϦʔϜม׵
    myError

    View full-size slide

  46. 1
    Observable.create(೚ҙͷॲཧ)
    completed
    σʔλετϦʔϜͷྲྀΕ
    create(೚ҙͷॲཧ)
    ετϦʔϜม׵
    3 5 7 9

    View full-size slide

  47. map: ετϦʔϜΛม׵͢Δ
    map(_:((T) -> R)) -> Observable
    ೚ҙͷܕɾ஋ͷετϦʔϜΛՃ޻ͯ͠ผͷܕɾ஋ͷετϦʔ
    Ϝʹม׵͠·͢ɻ

    View full-size slide

  48. filter: ετϦʔϜΛϑΟϧλ͢Δ
    filter(_:((T) -> Bool)) -> Observable
    ྲྀΕ͖ͯͨετϦʔϜΛҾ਺ͷؔ਺Ͱॱ൪ʹॲཧ͠ɺ
    ݁Ռ͕true ʹͳΔσʔλ͚ͩͷετϦʔϜʹม׵͠·͢ɻ

    View full-size slide

  49. flatMap: ετϦʔϜΛม׵͢
    Δ
    flatMap(_:((T) -> Observable)) -> Observable
    ೚ҙͷܕɾ஋ͷετϦʔϜΛՃ޻ͯ͠ผͷܕɾ஋ͷετϦʔ
    Ϝʹม׵͠·͢ɻmapͱ΄΅ಉ͡Ͱ͕͢ɺΫϩʔδϟͷฦΓ
    ஋͕Observableʹͳ͍ͬͯΔ఺͕ҟͳΓ·͢ɻ

    View full-size slide

  50. 1 completed
    2 3
    flatMap({x in Observable.of(x, x*10)})
    1 completed
    2 3
    10 20 30
    completed
    completed
    completed
    1 completed
    2 3
    10 20 30

    View full-size slide

  51. filter, map, flatMap ͷྫ

    View full-size slide

  52. map ͱ flatMap ͷ࢖͍෼͚
    map … ಉظతʹॲཧͰ͖Δσʔλม׵ʹ࢖͍·͢ɻ
    ྫɿStringܕ -> URLܕɺInt ܕΛॱ൪ʹܭࢉͯ͠ม׵͢Δ
    flatMap … ඇಉظॲཧͳͲɺผͷετϦʔϜΛ࢖ͬͯม׵͢Δ
    ࣌ʹ࢖͍·͢ɻ
    ྫɿtwitterͷπΠʔτIDετϦʔϜΛflatMapͯ͠ɺॱ൪ʹAPI
    Λୟ͍ͯ಺༰Λऔಘͯ͠λΠϜϥΠϯදࣔ͢Δ

    View full-size slide

  53. map ͱ subscribe ͷ࢖͍෼͚
    ʢ؆୯ͳઆ໌ʣ
    ͲͪΒͰ΋onNext ʹ͍ͭͯॲཧ͕ग़དྷ·͕͢ɺ
    subscribe()͸ܨ͍ͩετϦʔϜΛಈ͔࢝͠ΊΔͱ͍͏େࣄͳ໾ׂ͕͋Γ·͢ɻ
    (ετϦʔϜม׵ͷऴ୺)
    subscribe() ͸ετϦʔϜม׵Ͱͳ͘ετϦʔϜ࣮ߦʹͳΔͨΊɺObservable
    Ͱͳ͘DisposableΛฦ͠·͢ɻ͜ͷͨΊmapͱҧ͍͏͠ΖʹετϦʔϜΛͭͳ
    ͛Δ͜ͱ͕Ͱ͖·ͤΜɻ

    View full-size slide

  54. 1 completed
    σʔλετϦʔϜͷྲྀΕ
    Observable.from([1, 2, 3, 4, 5])
    ετϦʔϜม׵
    2 3 4 5
    filter({ x in x % 2 == 1})
    1 completed
    3 5
    map({ x in “v\(x)”)
    “v1” completed
    “v3” “v5”
    TVCTDSJCF

    “v1” completed
    “v3” “v5”

    View full-size slide

  55. ଞʹ΋
    ศརͳΦϖϨʔλ͕
    ͨ͘͞Μ͋Γ·͢
    ࢀߟɿ
    http://reactivex.io/documentation/operators.html
    http://rxmarbles.com/

    View full-size slide

  56. ศརͳΦϖϨʔλͷҰྫ
    • zip, merge, combineLatest … 

    ෳ਺ͷετϦʔϜΛฒྻʹಈ͔ͯ̍ͭ͠ͷετϦʔϜʹ·ͱΊΔɻ
    • catchError …

    Τϥʔ͕ൃੜͨ͠ͱ͖ʹϦΧόϦॲཧΛͯ͠onNext ʹม׵͢Δɻ࠶౓ΤϥʔΛ౤͛Δ(throw)͜ͱͰɺผͷΤ
    ϥʔʹม׵͢Δ͜ͱ΋Ͱ͖Δ
    • distinctUntilChanged …

    ॏෳআ֎ɻετϦʔϜதʹมߋ͕͋ͬͨͱ͖͚ͩ௨஌͢Δ
    • throttle, debounce …

    ࿈ଧ๷ࢭʹ࢖͑ΔϑΟϧλɻσʔλΛड͚औͬͨΒҰఆ࣌ؒؒΛ։͚ͳ͍ͱ͕࣍ൃՐ͠ͳͨ͘͠Γ͢Δ
    • toArray … 

    ഑ྻʹ·ͱΊΔɻcompleted·Ͱ଴ͬͯ̍ͭͷ഑ྻʹม׵͢Δ
    • groupBy …

    ετϦʔϜͷσʔλΛ޷͖ͳ৚݅ͰάϧʔϓԽ͢Δ

    View full-size slide

  57. RxSwift/RxCocoa ͷ
    Πϯετʔϧ
    ᶃ CocoaPods → Podfile:
    ᶄ pod install
    ᶅ .swift ϑΝΠϧͰimport
    ※Carthage / Swift Package Manager ʹ΋ରԠͯ͠·͢ɻ
    https://github.com/ReactiveX/RxSwift
    3Y$PDPBʹ͍ͭͯ͸ޙड़

    ·ͨ͸

    View full-size slide

  58. Hello, Rx world
    ˠ

    View full-size slide

  59. Hello, Rx world2
    ˠ

    View full-size slide

  60. Hello, Rx world3
    ˣ

    View full-size slide

  61. RxCocoa ʹ͍ͭͯ
    RxSwift ͷαϒηοτతͳϥΠϒϥϦͰ͢ɻ
    RxSwift ͕७ਮͳ Rx ࣮૷ͰɺRxCocoa ͸ iOS/Mac։ൃʹศརͳ෇ଐΫϥεɾ֦ு
    Λఏڙ͍ͯ͠·͢ɻ
    RxCocoa Λ࢖͏ʹ͸ RxSwift ͱ͸ผʹϥΠϒϥϦΠϯετʔϧ͕ඞཁʹͳΓ·͢ɻ
    > pod ‘RxCocoa'
    Ͱ pod install ͯ͠ɺswiftίʔυ಺Ͱ import RxCocoa Λॻ͍͓ͯ͘ͱ࢖͑·͢ɻ

    View full-size slide

  62. RxCocoa ͕ఏڙ͢Δػೳͷྫ

    View full-size slide

  63. UIKit ఏڙΫϥεͷ rx Խ
    UIView, URLSession ͳͲUIKit,͕ఏڙ͢ΔΫϥεͷϓϩύςΟɾΠϕϯτ
    ΛrxԽͯ͠ɺObservableΠϯελϯεͱͷ૬ޓόΠϯυΛՄೳʹ͢Δɻ
    ྫɿ
    - ਺ࣈΛςΩετೖྗͨ͠Βϥϕϧʹܭࢉ݁ՌΛදࣔ͢Δɻ
    - ϘλϯΛԡͨ͠ΒAPI௨৴ͯ͠ɺAPI࣮ߦ݁ՌΛϥϕϧʹදࣔ͢Δɻ
    - URLSession ͷAPI௨৴݁ՌJSONΛ੔ܗͯ͠ϓϩϑΟʔϧը໘ʹදࣔ

    View full-size slide

  64. ஌͓ͬͯ͘ͱྑ͍͔΋ɻ
    • RxAlamofire ͳͲ୳ͤ͹ઌਓ͕RxԽͯ͘͠
    ΕͯΔ΋ͷɺެ։͞ΕͯΔίʔυ͕݁ߏ͋
    Γ·͢ɻ

    View full-size slide

  65. ·ͱΊ
    • σʔλͷ΍ΓऔΓΛετϦʔϜͱͯ͠ߟ͑Α͏
    • ͱΓ͋͑ͣRxͷετϦʔϜ(Observable)ʹͰ͖ͨ
    Βͬͪ͜ͷ΋ͷɻͲ͏Ճ޻ͯ͠໨తͷܗࣜʹ͢Δ
    ͔Λߟ͑Α͏ɻ
    • RxΛۃΊͨΒϩδοΫ͕γϯϓϧʹ෭࡞༻ͳ͘ɺ
    εϚʔτʹॻ͚ΔΑ͏ʹͳΔɻඇಉظॲཧ΋ා͘
    ͳ͍ʢ͸ͣʣ

    View full-size slide

  66. ͍͞͝ʹ
    • tech vein Ͱ͸͜ΜͳΞϓϦΤϯδχΞɺ

    ϑϦʔϥϯεɾύʔτφʔ༷Λืू͍ͯ͠·͢ɻ
    • RxSwift, RxJava Λ࢖͍͍ͨ
    • MVVMɺ CleanArchitectureɺ DI Λ׆༻ͨ͠ઃܭʹڵຯ͕͋Δ
    ڵຯ͕͋Δํɺͪΐͬͱ࿩Λฉ͍ͯΈ͍ͨํ

    ͨͩ3Yͷ࿩Λ΋ͬͱฉ͖͍ͨํ΋

    ͥͻ͓੠͕͚Լ͍͞ʂ

    View full-size slide

  67. ࢀߟ
    • ࠓճͷαϯϓϧίʔυஔ͖৔ɹʴ RxCocoa Λ࢖ͬͨ؆୯ͳΞϓ
    Ϧ
    • GitHub:

    https://github.com/ecoopnet/rxswift-beginner
    • ࢀߟهࣄ
    • Կͱͳ͘RxJavaΛ࢖ͬͯΔ͚Ͳਖ਼௚Α͘෼͔ͬͯͳ͍ਓ͕ಡ
    Ήͱྑͦ͞͏ͳهࣄɾجૅฤ

    https://qiita.com/k-mats/items/4d374460a3f6284dd09f

    View full-size slide

  68. ͕࣌ؒ͋Ε͹αϯϓϧΞϓϦσϞ

    View full-size slide

  69. ͝ਗ਼ௌ
    ͋Γ͕ͱ͏͍͟͝·ͨ͠

    View full-size slide

  70. Driver ͷ࢖͍ํ͸ Observable ͱ΄΅ಉ͡

    View full-size slide

  71. Driver ͷ࢖͍ํ͸ Observable ͱ΄΅ಉ͡ʁ
    →Τϥʔॲཧ͕ཁΒͳ͍౳ͷࡉ͔͍ҧ͍͸͋Δ

    View full-size slide

  72. Driver (ิ଍)
    ۩ମతʹ͸ observable.asDriver(onErrorJustReturn: “”) ͱ͢Δͱ
    - UIεϨουͰॲཧɿ
    .observeOn(MainScheduler.instance)
    - Cold -> Hot ม׵ɿ
    .share(replay:1)
    - Τϥʔ࣌ʹ؂ࢹ͕ࢭ·ΔͷΛ๷͙ͨΊͷϦΧόϦॲཧɿ
    .catchError({ _ in “” })
    Λ·ͱΊͯ΍ͬͯ͘ΕΔΑ͏ʹͳΓ·͢ɻ
    ˎ

    View full-size slide