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

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

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

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

B6ce8bebf30b97bac7624b0c6622352e?s=128

Kazuhiro Sakamoto

April 14, 2016
Tweet

Transcript

  1. RxSwiftΛϓϩμΫτ ʹಋೖͯ͠Έͨ࿩ @kazu0620

  2. ࣗݾ঺հ ࡔຊ ࿨େ( @kazu0620 ) Sansanגࣜձࣾ Eightࣄۀ෦ॴଐ աڈʹݸਓͰ։ൃͨ͠ΞϓϦ - ൿີͷΞϧόϜ(40ສDL!)

    - ʹΌΜ͜(15ສDL!)
  3. agenda • iOSΞϓϦ Eightͷ͝঺հ • ͳͥRxSwiftͳͷ͔ • RxSwiftͰϋϚͬͨ͜ͱTOP 3 •

    RxSiwftΛ༻͍ͨΞϓϦͷઃܭʹ͍ͭͯ
  4. $PQZSJHIU˜4BOTBO *OD"MMSJHIUTSFTFSWFE

  5. $PQZSJHIU˜4BOTBO *OD"MMSJHIUTSFTFSWFE

  6. None
  7. ΦϯϥΠϯ໊ࢗަ׵ػೳ • NearbyAPIΛར༻ͯۙ͠ྡͷϢʔβʔͱΞϓ Ϧ্Ͱ໊ࢗަ׵Λ࣮ݱ͢Δ • ϦΞϧλΠϜʹঢ়ଶ͕มΘΓଓ͚ΔͨΊɺঢ় ଶ؅ཧ͸ඇৗʹෳࡶ • Model૚͔ΒPresentation૚·ͰRxSwiftΛར ༻࣮ͯ͠૷ͨ͠

  8. ͳͥRxSwiftͳͷ͔ʁ

  9. ͳͥRxSwiftͳͷ͔ʁ • એݴతʹهड़Ͱ͖Δ • Rx͸࣮͸ރΕٕͨज़Ͱ΋͋Δ • Rx+αͰඋΘ͍ͬͯΔػೳ܈͕ૉ੖Β͍͠

  10. ͳͥRxSwiftͳͷ͔ʁ • એݴతʹهड़Ͱ͖Δ • Rx͸࣮͸ރΕٕͨज़Ͱ΋͋Δ • Rx+αͰඋΘ͍ͬͯΔػೳ܈͕ૉ੖Β͍͠

  11. “ϓϩάϥϛϯάͷඞཁ͸͋Γ·ͤΜɻϓϩ άϥϜ͕Ͳ͏ಈ͘΂͖͔Λఆٛͯ͠ɼதؒ ঢ়ଶ΍༧૝֎ͷঢ়گ΁ͷରॲ͸ίϯϐϡʔ λʹ೚ͤΕ͹͍͍ͷͰ͢ɻ” By Krunoslav Zaher

  12. “ඒ͍͠ͱ͍͏ͷ͸ɺRx͸ͻͱͭͷจͰઆ໌ Ͱ͖Δ͔ΒͰ͢ɻObservable<T>͕ཁૉT ͷobservableγʔέϯεΛදݱ͍ͯͯ͠ɼ ͦͷsubscribeϝιουΛίʔϧ͢Δ͜ͱ ͰɼγʔέϯεͷཁૉΛड͚औΔࣗ਎ͷΦ ϒβʔόΛొ࿥͢Δ͜ͱ͕Ͱ͖·͢” By Krunoslav Zaher

  13. None
  14. ྫ͑͹ɺ 3ඵؒʹ1ճɺ
 όοΫάϥ΢ϯυεϨουͰɺ
 αʔόʔͱ௨৴ͯ͠ϢʔβʔͷList৘ใΛऔಘ͠ɺ
 ৽͍͠Ϣʔβʔͷ৘ใ͕Listʹ֨ೲ͞Ε͍ͯͨͱ͖ͷΈɺ
 ϢʔβʔͷεςʔλεΛνΣοΫ͠৚݅Λຬ͍ͨͯ͠Ε͹ɺ
 ͦͷϢʔβʔͷৄࡉ৘ใΛ͞ΒʹAPI͔Βऔಘ͠ɺ
 UIʹද͍ࣔͨ͠ʂ

  15. • ͜Ε͔Βى͖ಘΔग़དྷࣄͷγʔέϯεͱɺͦͷܭࢉํ๏Λఆ͍ٛͯ͠Δɻ • ͜ͷ࣌఺Ͱ͸ʮఆٛʯ͚ͨͩ͠(࣮ࡍͷॲཧ͕૸Δͷ͸ߪಡ<Subscirbe>͞ΕͨλΠϛϯά) • ෳࡶͳॲཧ͕ͩɺϝϯόม਺Λར༻ͨ͠Γঢ়ଶΛอ࣋ͨ͠Γɺͱ͍͏͜ͱ͸Ұ੾ඞཁ͕ͳ͍

  16. γʔέϯεʹର͢Δૢ࡞ • Observable͸γʔέϯεͰ͋Δ • γʔέϯεͳͷͰɺArrayͱಉ༷͡ʹແ਺ʹ͋Δ ΦϖϨʔλʔΛ࢖ͬͯૢ࡞͢Δ͜ͱ͕ग़དྷΔ (map, flatMap, filter, take,

    distinct…) • γʔέϯεͷܭࢉํ๏Λࣗ෼Ͱఆٛ(৽͍͠Φϖ ϨʔλΛ௥Ճ͢Δ)͢Δ͜ͱ΋؆୯ʹग़དྷΔ
  17. ͳͥRxSwiftͳͷ͔ʁ • એݴతʹهड़Ͱ͖Δ • Rx͸࣮͸ރΕٕͨज़Ͱ΋͋Δ • Rx+αͰඋΘ͍ͬͯΔػೳ܈͕ૉ੖Β͍͠

  18. ͳͥRxSwiftͳͷ͔ʁ • એݴతʹهड़Ͱ͖Δ • Rx͸࣮͸ރΕٕͨज़Ͱ΋͋Δ • Rx+αͰඋΘ͍ͬͯΔػೳ܈͕ૉ੖Β͍͠

  19. ͏Θͬ…
 RxSwiftͷ஌ݟগͳ͗͢…?

  20. None
  21. Reactive Extensionsͷྺ࢙ • 2009೥11݄ ϚΠΫϩιϑτ͕.Net Framework ্ͷϥΠϒϥϦͱ࣮ͯ͠ݧతʹެ։ • 2011೥11݄ ਖ਼ࣜͳ੡඼ͱͯ͠ঢ֨ɺ.NET

    C# ͷੈքͰར༻͞Ε࢝ΊΔ • ͦͷޙɺRxJS, RxJavaͱଞͷݴޠʹ΋Ҡ২͞Ε ࢝ΊΔ
  22. None
  23. Rxશମͷ஝ੵ͞Εͨ஌ݟΛ࢖͑Δ • Observable, Subscribe, Subject౳ͷجຊతͳ֓೦͸શͯڞ௨ • ΦϖϨʔλʔͷ໊લɾ༻๏΋(΄΅)ଞͷRxγϦʔζͱಉ͡ • Θ͔Βͳ͍ݴ༿͕͋ͬͨͱ͖͸ɺଞͷݴޠͷ৘ใΛάάΔͱେྔͷ஌ݟΛ ಘΒΕΔ


    (ex: [Rx hotม׵] [RxJS Subject][RxJava xxxx]ͳͲ) • ਺೥લͷC#ͷهࣄͱ͔ʹॿ͚ΒΕΔ͜ͱଟʑ͋Γ • Eightͷ৔߹͸andorid͕RxJavaΛઌߦಋೖ͓ͯ͠Γɺͦ͜Ͱͷ஌ݟ΋ ϑΟʔυόοΫͯ͠΋Β͏͜ͱ͕ग़དྷͨ
  24. ͳͥRxSwiftͳͷ͔ʁ • એݴతʹهड़Ͱ͖Δ • Rx͸࣮͸ރΕٕͨज़Ͱ΋͋Δ • Rx+αͰඋΘ͍ͬͯΔػೳ܈͕ૉ੖Β͍͠

  25. ͳͥRxSwiftͳͷ͔ʁ • એݴతʹهड़Ͱ͖Δ • Rx͸࣮͸ރΕٕͨज़Ͱ΋͋Δ • Rx+αͰඋΘ͍ͬͯΔػೳ܈͕ૉ੖Β͍͠

  26. 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͍ͨ͠ͱ͖ͳͲඇৗʹศ རɻ
  27. RxSwiftಠࣗͷ֦ு RxCocoaͷbindingػߏɺDriverͷ֓೦ɺVariableͷ֓೦ͳͲʹ͍ͭ ͯ͸ɺଞͷݴޠͷReactive Extensionsͷ஌ݟ͕ಘΒΕͳ͍ͷͰ஫ ҙɻRxSwiftͷDocumentΛಡΉɺఆٛՕॴͷίϝϯτΛݟΔͳͲ͠ ͨํ͕Θ͔Γ΍͍͢ɻ ͨͩɺRx্ͷ֓೦ʹରͯ͠Ͳ͏͍͏ૢ࡞Λͨ͠ΓWrap͢Δ͔ௐ΂Δ ͨΊʹଞͷݴޠͷ஌ݟΛੜ͔͢͜ͱ͸Ͱ͖Δɻ
 
 ྫ͑͹ɺVariable͸BehaviorSubjectΛWrapͯ͠Δ͕ɺ

    BehaviorSubjectͷڍಈ͸ଞͷݴޠͷRx࣮૷ͱมΘΒͳ͍ɻ
  28. ͱ͸͍͑ɺ RxSwiftͰϋϚͬͨͱ͜TOP 3

  29. ୈ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? ͱݴΘΕͯ΋?????? 
 …ͦΜͳ࣌ʹಡΜͰྑ͔ͬͨྑࢿྉू
  30. ୈ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ͷղઆΛಡΜͰΔͱසൟʹݱΕΔ͕ɺ࠷ॳͦͷ֓೦͕Θ͔Γʹ͘͘ཧղ ʹۤ࿑ͨ͠ɻͷͰɺࢀߟʹͳͬͨࢿྉΛ·ͱΊɻ
  31. RxΛ༻͍ͨઃܭʹ͍ͭͯ
 RxͱClean Architecture

  32. ಺ଆͷ֊૚͸ɺ֎ଆͷ֊૚ʹ͍ͭͯؔ৺Λ࣋ͨͳ͍
 
 ྫ͑͹: 
 Entity͸Usecase, Presentersion૚ͷ࣮૷ʹґଘ͠ͳ͍
 Usecase͸Presentersion૚ͷ࣮૷ʹґଘ͠ͳ͍
 ֊૚͝ͱͷ੹຿ɾґଘؔ܎Λ໌֬ʹ෼͚Δ͜ͱͰɺ อक͠΍͍͢TestableͳίʔυΛॻ͘͜ͱ͕ग़དྷ Δɻ

    Clean Architecture
  33. Clean Architecture • Presentation Layer
 ϏδωεϩδοΫΛ࣮૷ͨ͠usecaseΛར༻͠ɺͦͷίʔϧόοΫͰมߋΛViewʹඳը͢Δ • Domain Layer
 ϏδωεϩδοΫΛUsecaseͱ࣮ͯ͠૷͢ΔɻUsecase͸UI΍֎ଆͷϑϨʔϜϫʔΫʹ͍ͭͯؔ৺Λ࣋ͭඞཁ͸

    ͳ͍ɻ·ͨUsecaseʹΑͬͯEntityΛɺViewͰར༻͢ΔͨΊͷModelʹม׵͢Δ࡞ۀ΋ߦ͏ɻ • Data Layer
 Repository PatternΛར༻ͯ͠ɺσʔλιʔεΛந৅Խ͢Δɻ֎ͷLayer͔Β͸ɺͲ͜ʹσʔλ͕อଘ͞ΕͯΔ͔ ͸ҙࣝͤͣѻ͏͜ͱ͕ग़དྷΔɻྫ͑͹Ωϟογϡ͕͋Ε͹ͦΕΛར༻͢Δɺͳ͚Ε͹API͔ΒσʔλΛҾ͘ɺͱ ͍ͬͨૢ࡞ΛFactoryΛ༻͍ͯந৅Խ͢ΔɺͳͲɻ
  34. ͜ΕΛRxͷੈքʹద༻͢Δͱʁ

  35. https://github.com/android10/Android- CleanArchitecture Android ͰɺRxΛClean Architecture ʹద༻͠ ͨૉ੖Β͍͠஌ݟ͕͋Δ

  36. None
  37. None
  38. Presentation Layer • ViewController͸ɺը໘ͷϥΠϑαΠΫϧɾભҠΛ؅ཧ͢Δ΋ͷͱͯ͠ͷΈѻ͏ɻ • Presentation Logic͸XXXPresenterʹ࣮૷͢Δɻ
 (Ex. UserListPresenter) •

    Presenter = Subscriber
 Presenter͸ViewΛอ࣋͠ɺUsecase͕ੜ੒ͨ͠ϏδωεϩδοΫ(Observable)Λ ߪಡ / Viewʹbind͢Δ • ObservableΛߪಡ/bind͍ͯ͠Ε͹ɺࣗಈతʹঢ়ଶͷมԽ͕൓ө͞ΕΔͷͰϩδο Ϋ͸ඇৗʹബ͘ͳΔ • ౰વϏδωεϩδοΫ͕͜ͷ૚ʹ௚઀هड़͞ΕΔ͜ͱ͸ͳ͍
  39. None
  40. Domain Layer • Usecaseͱͯ͜͜͠ʹϏδωεϩδοΫΛهड़͢Δ (Ex: GetUserListUsecase) • Repository͔Β͸Observableʹแ·ΕͨEntity͕౉ ͬͯ͘Δ(Ex: Observable<UserList>)

    • EntityͷObservableΛϏδωεϧʔϧʹԠͯ͡ ༷ʑͳΦϖϨʔλΛར༻ͯ͠Transform͢Δ
  41. None
  42. Data Layer • σʔλͷऔಘํ๏(API͔Β/Ωϟογϡ͔Β౳)ΛFactoryͰந৅Խ͠ɺඞཁ ͳDetaStoreΛRepositry͕ݺͼग़͢͜ͱʹΑΓಁաతʹѻ͑ΔΑ͏ʹ͢Δ
 • σʔλͷऔಘϩδοΫ͸Observable<T>Ͱ౉͞ΕΔ • ྫ͑͹UserDataRepository͸ɺ
 UserDataStoreFactoryʹঢ়گʹԠͯ͡ඞཁͳDataStore


    (CloudUserDataStore/DiscUserDataStore)Λੜ੒ͯ͠΋Β͍ɺ
 ੜ੒͞ΕͨStore͔ΒObservable<UserData>Λऔಘ͢Δ
  43. EightͰͷઃܭ • ؙ͝ͱ͜ͷઃܭΛऔΓೖΕͯΔΘ͚Ͱ͸ͳ͍ɻඞཁʹԠͯ͡֊૚Λઃܭɻ • ྫ͑͹໊ࢗަ׵ػೳͰ͸ɺΫϥΠΞϯτʹσʔλΛӬଓԽͨ͠ΓΩϟογϡͨ͠Γɺ ͱ͍ͬͨૢ࡞͸ඞཁͳ͔ͬͨͷͰɺPresenterͱඞཁͳϏδωεϩδοΫʹԠͨ͡ Usecaseʹ෼͚͍ͯΔ
 
 Usecase: EntityΛऔಘՃ޻͢ΔObservableΛੜ੒

    
 
 Presenter: Usecase͕ੜ੒ͨ͠ObservableΛSuscribe͠ɺ
 ౉͖ͬͯͨEntityΛViewModel(Driver΍VariableΛpropertyͱͯ࣋ͭ͠)ʹม׵
 Viewʹbind͢Δɺͱ͍͏Α͏ͳྲྀΕɻ
 

  44. RxSwiftΛ ϓϩμΫτʹಋೖͨ͠ײ૝ͳͲ

  45. • ֶशίετ͸΍͸Γߴ͍ɻ࢖͍ํͱ͍͏ΑΓɺ֓೦తͳ΋ͷΛཧղ͢Δͷʹίε τ͕͔͔Δɻ • ίετ͸ߴ͍͕े෼ϖΠ͢Δ(ͱࢥͬͯΔ)ɻঢ়ଶΛγʔέϯεͱͯ͠એݴతʹॻ͚ Δ͜ͱɺͦΕʹରͯ͠Ғ͍ਓ͕ͨͪ࡞ͬͨΦϖϨʔλΛ༻͍ͯૢ࡞͕Ͱ͖Δͷ͸ ඇৗʹศརʢ͜͏͍͏ͷ͋ͬͨΒศརͩͳɺͬͯͷ͸େମطʹ͋Δʣɻͺͬͱݟ ໘౗ͩͳɺͱ͍͏ॲཧ͕1ߦͱ͔2ߦͰॻ͚Δ͜ͱ΋ɻ • (Rxʹݶͬͨ࿩Ͱ͸ͳ͍͕)σʔλόΠϯσΟϯά͸UI࣮૷͕ڪΖָ͘͠ʹͳΔɻ

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

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