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

RxSwiftをプロダクトに導入してみた話

 RxSwiftをプロダクトに導入してみた話

4月14日 RxSwift勉強会の発表資料です!

Kazuhiro Sakamoto

April 14, 2016
Tweet

More Decks by Kazuhiro Sakamoto

Other Decks in Programming

Transcript

  1. Rxશମͷ஝ੵ͞Εͨ஌ݟΛ࢖͑Δ • Observable, Subscribe, Subject౳ͷجຊతͳ֓೦͸શͯڞ௨ • ΦϖϨʔλʔͷ໊લɾ༻๏΋(΄΅)ଞͷRxγϦʔζͱಉ͡ • Θ͔Βͳ͍ݴ༿͕͋ͬͨͱ͖͸ɺଞͷݴޠͷ৘ใΛάάΔͱେྔͷ஌ݟΛ ಘΒΕΔ


    (ex: [Rx hotม׵] [RxJS Subject][RxJava xxxx]ͳͲ) • ਺೥લͷC#ͷهࣄͱ͔ʹॿ͚ΒΕΔ͜ͱଟʑ͋Γ • Eightͷ৔߹͸andorid͕RxJavaΛઌߦಋೖ͓ͯ͠Γɺͦ͜Ͱͷ஌ݟ΋ ϑΟʔυόοΫͯ͠΋Β͏͜ͱ͕ग़དྷͨ
  2. RxSwiftಠࣗͷ֦ு • RxCococa
 UIKitͷΫϥεͷ֤ϓϩύςΟɾΠϕϯτΛRxͰར༻Ͱ͖ΔΑ͏ʹ
 Wrapͯ͘͠Ε͍ͯΔɻ͜Εͷ͓ӄͰUIViewͷঢ়ଶมԽ΍ΠϕϯτΛ
 ಠ࣮ࣗ૷͢Δ͜ͱͳ͘؆୯ʹbindingͰ͖Δɻ • Driver
 ObservableΛUIύʔπʹbind͢ΔͨΊͷػߏɻObservableΛDriverʹม׵͢Δ͜ͱͰɺ
 [ϝΠϯεϨουͰߪಡ͞ΕΔ]


    [ΤϥʔΛग़ྗ͠ͳ͍]
 [ෳ਺ͷViewʹbindͯ͠΋1ͭͷSubscription͚͕ͩੜ੒ɾڞ༗͞ΕΔ(Hotม׵͞ΕΔ)]
 ͱ͍ͬͨɺViewʹObservableΛద߹͢ΔͨΊʹඞཁͳॲཧΛཪଆͰߦͬͯ͘ΕΔɻ • Variable
 Subject(BehaviorSubject)ͷWrapperɻSubjectͱҧͬͯɺऴྃͨ͠ΓΤϥʔΛग़ྗ͢Δɺͱ͍͏ ͜ͱ͕ͳ͍ɻྫ͑͹ViewModelʹ஋Λอ࣋ͯ͠ɺͦͷมԽΛObserve͍ͨ͠ͱ͖ͳͲඇৗʹศ རɻ
  3. ୈ1Ґ
 ԿΛͲ͏ֶश͢Ε͹ྑ͍͔Θ͔Βͳ͍ ʲ຋༁ʳ͋ͳ͕ͨٻΊ͍ͯͨϦΞΫςΟϒϓϩάϥϛϯάೖ໳
 http://ninjinkun.hatenablog.com/entry/introrxja
 
 SwiftͰωΠςΟϒͳϦΞΫςΟϒؔ਺ܕϓϩάϥϛϯάΛ࣮ݱ͢ΔRxSwift
 http://www.infoq.com/jp/news/2016/04/rxswift-reactive-swift
 
 RxJS Advent

    Calendar
 http://qiita.com/advent-calendar/2015/rxjs
 
 RxSwift Getting Started
 https://github.com/ReactiveX/RxSwift/blob/master/Documentation/GettingStarted.md
 
 RxSwiftϦϙδτϦ(αϯϓϧΛͻͨ͢ΒಡΉͱྑ͍)
 https://github.com/ReactiveX/RxSwift
 
 
 
 Observable? Sbscribe? Hot? ͱݴΘΕͯ΋?????? 
 …ͦΜͳ࣌ʹಡΜͰྑ͔ͬͨྑࢿྉू
  4. ୈ3Ґ SubscribeOn Subscribe͢Δ࣌ͷεϨουΛࢦఆ͢Δͷ͔ͱࢥ͏τϥοϓɻ࣮͸ɺιʔε (ى఺)ͱͳOservableͷ࣮ߦεϨουΛࢦఆ͢ΔɻSubscribe͢ΔεϨουΛ ࢦఆ͚ͨ͠Ε͹ɺSubscribe͢Δ௚લʹObserveOn͢Δɻ ୈ2Ґ Hotม׵ RxͷHotͱColdʹ͍ͭͯ
 http://qiita.com/toRisouP/items/f6088963037bfda658d3
 


    Rxೖ໳ (13) - HotͱCold
 http://blog.xin9le.net/entry/2012/01/18/105003
 
 [RxSwift] shareReplayΛͪΌΜͱॻ͍͓ͯߦّྑ͘ετϦʔϜΛߪಡ͠Α͏
 http://qiita.com/kazu0620/items/bde4a65e82a10bd33f88 RxͷղઆΛಡΜͰΔͱසൟʹݱΕΔ͕ɺ࠷ॳͦͷ֓೦͕Θ͔Γʹ͘͘ཧղ ʹۤ࿑ͨ͠ɻͷͰɺࢀߟʹͳͬͨࢿྉΛ·ͱΊɻ
  5. Clean Architecture • Presentation Layer
 ϏδωεϩδοΫΛ࣮૷ͨ͠usecaseΛར༻͠ɺͦͷίʔϧόοΫͰมߋΛViewʹඳը͢Δ • Domain Layer
 ϏδωεϩδοΫΛUsecaseͱ࣮ͯ͠૷͢ΔɻUsecase͸UI΍֎ଆͷϑϨʔϜϫʔΫʹ͍ͭͯؔ৺Λ࣋ͭඞཁ͸

    ͳ͍ɻ·ͨUsecaseʹΑͬͯEntityΛɺViewͰར༻͢ΔͨΊͷModelʹม׵͢Δ࡞ۀ΋ߦ͏ɻ • Data Layer
 Repository PatternΛར༻ͯ͠ɺσʔλιʔεΛந৅Խ͢Δɻ֎ͷLayer͔Β͸ɺͲ͜ʹσʔλ͕อଘ͞ΕͯΔ͔ ͸ҙࣝͤͣѻ͏͜ͱ͕ग़དྷΔɻྫ͑͹Ωϟογϡ͕͋Ε͹ͦΕΛར༻͢Δɺͳ͚Ε͹API͔ΒσʔλΛҾ͘ɺͱ ͍ͬͨૢ࡞ΛFactoryΛ༻͍ͯந৅Խ͢ΔɺͳͲɻ
  6. Presentation Layer • ViewController͸ɺը໘ͷϥΠϑαΠΫϧɾભҠΛ؅ཧ͢Δ΋ͷͱͯ͠ͷΈѻ͏ɻ • Presentation Logic͸XXXPresenterʹ࣮૷͢Δɻ
 (Ex. UserListPresenter) •

    Presenter = Subscriber
 Presenter͸ViewΛอ࣋͠ɺUsecase͕ੜ੒ͨ͠ϏδωεϩδοΫ(Observable)Λ ߪಡ / Viewʹbind͢Δ • ObservableΛߪಡ/bind͍ͯ͠Ε͹ɺࣗಈతʹঢ়ଶͷมԽ͕൓ө͞ΕΔͷͰϩδο Ϋ͸ඇৗʹബ͘ͳΔ • ౰વϏδωεϩδοΫ͕͜ͷ૚ʹ௚઀هड़͞ΕΔ͜ͱ͸ͳ͍
  7. • ֶशίετ͸΍͸Γߴ͍ɻ࢖͍ํͱ͍͏ΑΓɺ֓೦తͳ΋ͷΛཧղ͢Δͷʹίε τ͕͔͔Δɻ • ίετ͸ߴ͍͕े෼ϖΠ͢Δ(ͱࢥͬͯΔ)ɻঢ়ଶΛγʔέϯεͱͯ͠એݴతʹॻ͚ Δ͜ͱɺͦΕʹରͯ͠Ғ͍ਓ͕ͨͪ࡞ͬͨΦϖϨʔλΛ༻͍ͯૢ࡞͕Ͱ͖Δͷ͸ ඇৗʹศརʢ͜͏͍͏ͷ͋ͬͨΒศརͩͳɺͬͯͷ͸େମطʹ͋Δʣɻͺͬͱݟ ໘౗ͩͳɺͱ͍͏ॲཧ͕1ߦͱ͔2ߦͰॻ͚Δ͜ͱ΋ɻ • (Rxʹݶͬͨ࿩Ͱ͸ͳ͍͕)σʔλόΠϯσΟϯά͸UI࣮૷͕ڪΖָ͘͠ʹͳΔɻ

    UIͷϩδοΫࣗମബ͘ͳΔͷͰɺঢ়ଶʹؔ͢Δ࢓༷ͷมߋ͕͋ͬͨ৔߹΋Model ૚͚ͩΛमਖ਼͢Ε͹Α͘ɺPresentation૚͸Կ΋มߋ͢Δඞཁ͕ͳ͍ɻ • ଞͷݴޠͰRxΛ৮Δͱ͖ʹɺֶΜͩ஌ࣝΛͦͷ··࢖͑Δͱ͍͏ͷ΋େ͖͍ɻ Androidͷ࣮૷ͷϓϧϦΫΤετΛݟͯɺԿΛ͍ͯ͠Δ͔ཧղͰ͖ͨΓɺRxʹͭ ͍ͯվΊͯൃݟग़དྷͨΓ͢Δɻ։ൃݴޠ΍OSͷนΛ௒͑ͨීวతͳ஌ࣝͱֶͯ͠ ͿՁ஋͕͋Δɻ • RxΛֶͿ͜ͱࣗମ΍ͬͺΓ৽઱Ͱָ͍͠ɻ
  8. $PQZSJHIU˜4BOTBO *OD"MMSJHIUTSFTFSWFE  4BOTBO͸Ұॹʹ৽͍͠Ձ஋Λ࡞͍ͬͯ͘ ஥ؒΛ͕͍ͯ͞͠·͢ɻ 3VCZ 3VCZPO3BJMT ʢ8FCΞϓϦέʔγϣϯʣ $ɼ"41/&5.7$ ʢ8FCΞϓϦέʔγϣϯʣ

    J04"OESPJEΞϓϦ   ݸਓ޲໊͚ࢗ؅ཧΞϓϦʮ&JHIUʯ   ໊ࢗσʔλԽ෼ࢄॲཧγεςϜ   ๏ਓ޲໊͚ࢗ؅ཧαʔϏεʮ4BOTBOʯ   ๏ਓ޲໊͚ࢗ؅ཧαʔϏε ʮ4BOTBOʯ   ݸਓ޲໊͚ࢗ؅ཧΞϓϦʮ&JHIUʯ ΤϯδχΞืूத 4BOTBO࠾༻ ݕࡧ SFDSVJU!TBOTBODPN·Ͱ ͓ؾܰʹ͝࿈བྷ͍ͩ͘͞ɻ ڵຯͷ͋Δํ͸