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. ࣗݾ঺հ גࣜձࣾ tech vein ୅ද ழມ ॆԝ (͍ͷ·ͨ ΈͭͻΖ) ձࣾHP:

    https://www.tech-vein.com/ private twitter: @ino2222
  2. RxSwiftͱ͸ • ֤ݴޠͰ࣮૷͞Ε͍ͯΔʮRx(Reactive Extension)ʯ
 ͷSwift޲͚ϥΠϒϥϦ • Reactive Extension … ΋ͱ΋ͱ͸


    C#.NET Ͱ࢖ΘΕͩͨ͠ Rx.NET ͕ΦϦδφϧ • Java(RxJava), JavaScript(RxJS), C++(RxCpp), Ruby(Rx.rb), PHP(RxPHP) ͳͲͳͲଟ਺
  3. ReactiveXͱ͸Կ͔ʁ ReactiveX = Reactive Extension Reactive Programming ʹ൓ԠతϓϩάϥϛϯάΛ͢Δͨ Ίͷ Extension

    = ֦ு ReactiveX͸ - σβΠϯύλʔϯͷ Observable ύλʔϯ - σβΠϯύλʔϯͷ Iterator ύλʔϯ - ؔ਺ܕϓϩάϥϛϯά Λ૊Έ߹Θͤͯ࡞ΒΕͨϥΠϒϥϦͷ͜ͱ
  4. + subscribe() Observable + onNext() + onError() + onCompleted() +

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

    subscribe() Observable + onNext() + onError() + onCompleted() + subscribe() Subject ܧঝ + dispose() Disposable subscribe() ͷ໭Γ஋
  6. ετϦʔϜͷΠϕϯτ • next
 σʔλ͕ਖ਼͘͠ྲྀΕ͖ͯͨͱ͍͏Πϕϯτɻ೚ҙͷܕͷσʔλΛҾ਺ʹ࣋ ͭɻ • error
 ετϦʔϜத͕Τϥʔ(ྫ֎)Ͱҟৗఀࢭͨ࣌͠ʹྲྀΕΔΠϕϯτɻൃੜͨ͠ ΤϥʔΛҾ਺ʹ࣋ͭɻ •

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

    next ετϦʔϜ͸ next ͰσʔλΛྲྀ͠ɺDPNQMFUFEͰऴྃ͢Δ DPNQMFUFE͕དྷͨΒɺͦͷઌ͸OFYUFSSPS͸ྲྀͤͳ͍ next next next FSSPS ྫ֎ ͕དྷͨΒɺͦͷઌ͸OFYUDPNQMFUFEFSSPS͸ྲྀͤͳ͍ error
  8. RxSwift ͷ Subject࢖͍෼͚ ɾPublishSubject ɹաڈͷσʔλΛอ࣋͠ͳ͍Subjectɻ ɹར༻ྫɿϘλϯͷλοϓΠϕϯτ ɾBehaviorSubject ɹաڈͷσʔλͷ͏ͪ௚ۙͷ̍ͭΛอ͍࣋ͯ͠ΔSubjectɻ ɹར༻ྫɿεΠονON/OFFঢ়ଶɾϢʔβεςʔλεͳͲ ɾReplaySubject.create(bufferSize:

    Int) ɹաڈͷ௚ۙσʔλΛࢦఆόοϑΝʹೖΔ͚ͩอ͍࣋ͯ͠ΔSubjectɻ ɹόοϑΝαΠζ̌ͳΒPublishSubjectͱಉ͡ɻ ɹόοϑΝαΠζ̍ͳΒBehaviorSubjectͱಉ͡ɻ ɹར༻ྫɿࢥ͍͔ͭͳ͍…ཤྺܥͰ࢖͑Δ͔΋ʁ
  9. Disposable ௚༁͢Δͱɺഇغɾॲ෼Ͱ͖Δ΋ͷɻ ObservableΛ subscribe(ߪಡ) ͢Δͱ໭Γ஋ͱͯ͠ Disposable Λ ฦ͠·͢ɻ Disposableʹରͯ͠dispose()ΛݺͿ͜ͱͰɺඇಉظॲཧͷΩϟϯη ϧͳͲɺ؂ࢹ͢Δଆͱͯ͠ॲཧΛࢭΊΔࣄ͕Ͱ͖·͢ɻ

    ྫɿը໘Λดͨ͡Β௨৴Ωϟϯηϧͨ͠Γɺೖྗ଴ͪΛࢭΊͨΓ͢ Δɻ + subscribe() Observable + onNext() + onError() + onCompleted() + subscribe() Subject ܧঝ + dispose() Disposable subscribe() ͷ໭Γ஋
  10. + subscribe() Observable + onNext() + onError() + onCompleted() +

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

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

    subscribe() Subject ܧঝ + dispose() Disposable subscribe() ͷ໭Γ஋ subscribe(onNext: … ) ΠϕϯτΛҾ਺ͷΫϩʔδϟͰ଴ͪड͚Δ PO/FYU lIFMMPz  PO/FYU l3Yz  PO/FYU l8PSMEz  ʜ PO$PNQMFUFE ॲཧͷྲྀΕ
  13. + subscribe() Observable + onNext() + onError() + onCompleted() +

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

    subscribe() Subject ܧঝ + dispose() Disposable subscribe() ͷ໭Γ஋ subscribe(onNext: … ) dispose() ΠϕϯτΛҾ਺ͷΫϩʔδϟͰ଴ͪड͚Δ ॲཧΛࢭΊͨ͘ͳͬͨΒEJTQPTFΛݺͿ ॲཧͷྲྀΕ
  15. (x, y) (x2, y2) (x3, y3) ྫɿλονΠϕϯτͷ؂ࢹ Observable<Point>) ࣌ؒͷྲྀΕ (x4,

    y4) TVCTDSJCF EJTQPTF EJTQPTFE (x2, y2) (x3, y3) Disposable.dispose()
  16. (x, y) (x2, y2) (x3, y3) ྫɿλονΠϕϯτͷ؂ࢹ Observable<Point>) ࣌ؒͷྲྀΕ (x4,

    y4) TVCTDSJCF EJTQPTF EJTQPTFE (x2, y2) (x3, y3) EJTQPTFE CZEJTQPTF#BH Subscibe ॲཧ DisposeBag JOJU EFJOJU View %JTQPTBCMFΛ %JTQPTF#BHͷ தʹೖΕΔɻ %JTQPTF#BH͸ ഁغ͞ΕΔͱ தͷ%JTQPTBCMFΛ EJTQPTF͢Δɻ
  17. ετϦʔϜΛ࡞Δ ̍ … Subject Ϋϥε͔Β࡞Δ ̎ … from(), of(), just()

    ͳͲͰ೚ҙͷ഑ྻɾ஋Λ ม׵ͯ͠࡞Δ ̏ … Observable.create() Ͱ೚ҙͷॲཧ͔Β࡞Δ
  18. 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
  19. map ͱ flatMap ͷ࢖͍෼͚ map … ಉظతʹॲཧͰ͖Δσʔλม׵ʹ࢖͍·͢ɻ ྫɿStringܕ -> URLܕɺInt

    ܕΛॱ൪ʹܭࢉͯ͠ม׵͢Δ flatMap … ඇಉظॲཧͳͲɺผͷετϦʔϜΛ࢖ͬͯม׵͢Δ ࣌ʹ࢖͍·͢ɻ ྫɿtwitterͷπΠʔτIDετϦʔϜΛflatMapͯ͠ɺॱ൪ʹAPI Λୟ͍ͯ಺༰Λऔಘͯ͠λΠϜϥΠϯදࣔ͢Δ
  20. map ͱ subscribe ͷ࢖͍෼͚ ʢ؆୯ͳઆ໌ʣ ͲͪΒͰ΋onNext ʹ͍ͭͯॲཧ͕ग़དྷ·͕͢ɺ subscribe()͸ܨ͍ͩετϦʔϜΛಈ͔࢝͠ΊΔͱ͍͏େࣄͳ໾ׂ͕͋Γ·͢ɻ (ετϦʔϜม׵ͷऴ୺) subscribe()

    ͸ετϦʔϜม׵Ͱͳ͘ετϦʔϜ࣮ߦʹͳΔͨΊɺObservable Ͱͳ͘DisposableΛฦ͠·͢ɻ͜ͷͨΊmapͱҧ͍͏͠ΖʹετϦʔϜΛͭͳ ͛Δ͜ͱ͕Ͱ͖·ͤΜɻ
  21. 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”
  22. ศརͳΦϖϨʔλͷҰྫ • zip, merge, combineLatest … 
 ෳ਺ͷετϦʔϜΛฒྻʹಈ͔ͯ̍ͭ͠ͷετϦʔϜʹ·ͱΊΔɻ • catchError

    …
 Τϥʔ͕ൃੜͨ͠ͱ͖ʹϦΧόϦॲཧΛͯ͠onNext ʹม׵͢Δɻ࠶౓ΤϥʔΛ౤͛Δ(throw)͜ͱͰɺผͷΤ ϥʔʹม׵͢Δ͜ͱ΋Ͱ͖Δ • distinctUntilChanged …
 ॏෳআ֎ɻετϦʔϜதʹมߋ͕͋ͬͨͱ͖͚ͩ௨஌͢Δ • throttle, debounce …
 ࿈ଧ๷ࢭʹ࢖͑ΔϑΟϧλɻσʔλΛड͚औͬͨΒҰఆ࣌ؒؒΛ։͚ͳ͍ͱ͕࣍ൃՐ͠ͳͨ͘͠Γ͢Δ • toArray … 
 ഑ྻʹ·ͱΊΔɻcompleted·Ͱ଴ͬͯ̍ͭͷ഑ྻʹม׵͢Δ • groupBy …
 ετϦʔϜͷσʔλΛ޷͖ͳ৚݅ͰάϧʔϓԽ͢Δ
  23. RxSwift/RxCocoa ͷ Πϯετʔϧ ᶃ CocoaPods → Podfile: ᶄ pod install

    ᶅ .swift ϑΝΠϧͰimport ※Carthage / Swift Package Manager ʹ΋ରԠͯ͠·͢ɻ https://github.com/ReactiveX/RxSwift 3Y$PDPBʹ͍ͭͯ͸ޙड़ ·ͨ͸
  24. RxCocoa ʹ͍ͭͯ RxSwift ͷαϒηοτతͳϥΠϒϥϦͰ͢ɻ RxSwift ͕७ਮͳ Rx ࣮૷ͰɺRxCocoa ͸ iOS/Mac։ൃʹศརͳ෇ଐΫϥεɾ֦ு

    Λఏڙ͍ͯ͠·͢ɻ RxCocoa Λ࢖͏ʹ͸ RxSwift ͱ͸ผʹϥΠϒϥϦΠϯετʔϧ͕ඞཁʹͳΓ·͢ɻ > pod ‘RxCocoa' Ͱ pod install ͯ͠ɺswiftίʔυ಺Ͱ import RxCocoa Λॻ͍͓ͯ͘ͱ࢖͑·͢ɻ
  25. UIKit ఏڙΫϥεͷ rx Խ UIView, URLSession ͳͲUIKit,͕ఏڙ͢ΔΫϥεͷϓϩύςΟɾΠϕϯτ ΛrxԽͯ͠ɺObservableΠϯελϯεͱͷ૬ޓόΠϯυΛՄೳʹ͢Δɻ ྫɿ -

    ਺ࣈΛςΩετೖྗͨ͠Βϥϕϧʹܭࢉ݁ՌΛදࣔ͢Δɻ - ϘλϯΛԡͨ͠ΒAPI௨৴ͯ͠ɺAPI࣮ߦ݁ՌΛϥϕϧʹදࣔ͢Δɻ - URLSession ͷAPI௨৴݁ՌJSONΛ੔ܗͯ͠ϓϩϑΟʔϧը໘ʹදࣔ
  26. ͍͞͝ʹ • tech vein Ͱ͸͜ΜͳΞϓϦΤϯδχΞɺ
 ϑϦʔϥϯεɾύʔτφʔ༷Λืू͍ͯ͠·͢ɻ • RxSwift, RxJava Λ࢖͍͍ͨ

    • MVVMɺ CleanArchitectureɺ DI Λ׆༻ͨ͠ઃܭʹڵຯ͕͋Δ ڵຯ͕͋Δํɺͪΐͬͱ࿩Λฉ͍ͯΈ͍ͨํ
 ͨͩ3Yͷ࿩Λ΋ͬͱฉ͖͍ͨํ΋
 ͥͻ͓੠͕͚Լ͍͞ʂ
  27. ࢀߟ • ࠓճͷαϯϓϧίʔυஔ͖৔ɹʴ RxCocoa Λ࢖ͬͨ؆୯ͳΞϓ Ϧ • GitHub:
 https://github.com/ecoopnet/rxswift-beginner •

    ࢀߟهࣄ • Կͱͳ͘RxJavaΛ࢖ͬͯΔ͚Ͳਖ਼௚Α͘෼͔ͬͯͳ͍ਓ͕ಡ Ήͱྑͦ͞͏ͳهࣄɾجૅฤ
 https://qiita.com/k-mats/items/4d374460a3f6284dd09f
  28. Driver (ิ଍) ۩ମతʹ͸ observable.asDriver(onErrorJustReturn: “”) ͱ͢Δͱ - UIεϨουͰॲཧɿ .observeOn(MainScheduler.instance) -

    Cold -> Hot ม׵ɿ .share(replay:1) - Τϥʔ࣌ʹ؂ࢹ͕ࢭ·ΔͷΛ๷͙ͨΊͷϦΧόϦॲཧɿ .catchError({ _ in “” }) Λ·ͱΊͯ΍ͬͯ͘ΕΔΑ͏ʹͳΓ·͢ɻ ˎ