RxSwiftをプロダクトに導入してみた話
by
Kazuhiro Sakamoto
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
RxSwiftΛϓϩμΫτ ʹಋೖͯ͠Έͨ @kazu0620
Slide 2
Slide 2 text
ࣗݾհ ࡔຊ େ( @kazu0620 ) Sansanגࣜձࣾ Eightࣄۀ෦ॴଐ աڈʹݸਓͰ։ൃͨ͠ΞϓϦ - ൿີͷΞϧόϜ(40ສDL!) - ʹΌΜ͜(15ສDL!)
Slide 3
Slide 3 text
agenda • iOSΞϓϦ Eightͷ͝հ • ͳͥRxSwiftͳͷ͔ • RxSwiftͰϋϚͬͨ͜ͱTOP 3 • RxSiwftΛ༻͍ͨΞϓϦͷઃܭʹ͍ͭͯ
Slide 4
Slide 4 text
$PQZSJHIU4BOTBO *OD"MMSJHIUTSFTFSWFE
Slide 5
Slide 5 text
$PQZSJHIU4BOTBO *OD"MMSJHIUTSFTFSWFE
Slide 6
Slide 6 text
No content
Slide 7
Slide 7 text
ΦϯϥΠϯ໊ަػೳ • NearbyAPIΛར༻ͯۙ͠ྡͷϢʔβʔͱΞϓ Ϧ্Ͱ໊ަΛ࣮ݱ͢Δ • ϦΞϧλΠϜʹঢ়ଶ͕มΘΓଓ͚ΔͨΊɺঢ় ଶཧඇৗʹෳࡶ • Model͔ΒPresentation·ͰRxSwiftΛར ༻࣮ͯͨ͠͠
Slide 8
Slide 8 text
ͳͥRxSwiftͳͷ͔ʁ
Slide 9
Slide 9 text
ͳͥRxSwiftͳͷ͔ʁ • એݴతʹهड़Ͱ͖Δ • Rx࣮ރΕٕͨज़Ͱ͋Δ • Rx+αͰඋΘ͍ͬͯΔػೳ܈͕ૉΒ͍͠
Slide 10
Slide 10 text
ͳͥRxSwiftͳͷ͔ʁ • એݴతʹهड़Ͱ͖Δ • Rx࣮ރΕٕͨज़Ͱ͋Δ • Rx+αͰඋΘ͍ͬͯΔػೳ܈͕ૉΒ͍͠
Slide 11
Slide 11 text
“ϓϩάϥϛϯάͷඞཁ͋Γ·ͤΜɻϓϩ άϥϜ͕Ͳ͏ಈ͖͔͘Λఆٛͯ͠ɼதؒ ঢ়ଶ༧֎ͷঢ়گͷରॲίϯϐϡʔ λʹͤΕ͍͍ͷͰ͢ɻ” By Krunoslav Zaher
Slide 12
Slide 12 text
“ඒ͍͠ͱ͍͏ͷɺRxͻͱͭͷจͰઆ໌ Ͱ͖Δ͔ΒͰ͢ɻObservable͕ཁૉT ͷobservableγʔέϯεΛදݱ͍ͯͯ͠ɼ ͦͷsubscribeϝιουΛίʔϧ͢Δ͜ͱ ͰɼγʔέϯεͷཁૉΛड͚औΔࣗͷΦ ϒβʔόΛొ͢Δ͜ͱ͕Ͱ͖·͢” By Krunoslav Zaher
Slide 13
Slide 13 text
No content
Slide 14
Slide 14 text
ྫ͑ɺ 3ඵؒʹ1ճɺ όοΫάϥϯυεϨουͰɺ αʔόʔͱ௨৴ͯ͠ϢʔβʔͷListใΛऔಘ͠ɺ ৽͍͠Ϣʔβʔͷใ͕Listʹ֨ೲ͞Ε͍ͯͨͱ͖ͷΈɺ ϢʔβʔͷεςʔλεΛνΣοΫ݅͠Λຬ͍ͨͯ͠Εɺ ͦͷϢʔβʔͷৄࡉใΛ͞ΒʹAPI͔Βऔಘ͠ɺ UIʹද͍ࣔͨ͠ʂ
Slide 15
Slide 15 text
• ͜Ε͔Βى͖ಘΔग़དྷࣄͷγʔέϯεͱɺͦͷܭࢉํ๏Λఆ͍ٛͯ͠Δɻ • ͜ͷ࣌Ͱʮఆٛʯ͚ͨͩ͠(࣮ࡍͷॲཧ͕Δͷߪಡ͞ΕͨλΠϛϯά) • ෳࡶͳॲཧ͕ͩɺϝϯόมΛར༻ͨ͠Γঢ়ଶΛอ࣋ͨ͠Γɺͱ͍͏͜ͱҰඞཁ͕ͳ͍
Slide 16
Slide 16 text
γʔέϯεʹର͢Δૢ࡞ • ObservableγʔέϯεͰ͋Δ • γʔέϯεͳͷͰɺArrayͱಉ༷͡ʹແʹ͋Δ ΦϖϨʔλʔΛͬͯૢ࡞͢Δ͜ͱ͕ग़དྷΔ (map, flatMap, filter, take, distinct…) • γʔέϯεͷܭࢉํ๏ΛࣗͰఆٛ(৽͍͠Φϖ ϨʔλΛՃ͢Δ)͢Δ͜ͱ؆୯ʹग़དྷΔ
Slide 17
Slide 17 text
ͳͥRxSwiftͳͷ͔ʁ • એݴతʹهड़Ͱ͖Δ • Rx࣮ރΕٕͨज़Ͱ͋Δ • Rx+αͰඋΘ͍ͬͯΔػೳ܈͕ૉΒ͍͠
Slide 18
Slide 18 text
ͳͥRxSwiftͳͷ͔ʁ • એݴతʹهड़Ͱ͖Δ • Rx࣮ރΕٕͨज़Ͱ͋Δ • Rx+αͰඋΘ͍ͬͯΔػೳ܈͕ૉΒ͍͠
Slide 19
Slide 19 text
͏Θͬ… RxSwiftͷݟগͳ͗͢…?
Slide 20
Slide 20 text
No content
Slide 21
Slide 21 text
Reactive Extensionsͷྺ࢙ • 200911݄ ϚΠΫϩιϑτ͕.Net Framework ্ͷϥΠϒϥϦͱ࣮ͯ͠ݧతʹެ։ • 201111݄ ਖ਼ࣜͳͱͯ͠ঢ֨ɺ.NET C# ͷੈքͰར༻͞Ε࢝ΊΔ • ͦͷޙɺRxJS, RxJavaͱଞͷݴޠʹҠ২͞Ε ࢝ΊΔ
Slide 22
Slide 22 text
No content
Slide 23
Slide 23 text
Rxશମͷੵ͞ΕͨݟΛ͑Δ • Observable, Subscribe, Subjectͷجຊతͳ֓೦શͯڞ௨ • ΦϖϨʔλʔͷ໊લɾ༻๏(΄΅)ଞͷRxγϦʔζͱಉ͡ • Θ͔Βͳ͍ݴ༿͕͋ͬͨͱ͖ɺଞͷݴޠͷใΛάάΔͱେྔͷݟΛ ಘΒΕΔ (ex: [Rx hotม] [RxJS Subject][RxJava xxxx]ͳͲ) • લͷC#ͷهࣄͱ͔ʹॿ͚ΒΕΔ͜ͱଟʑ͋Γ • Eightͷ߹andorid͕RxJavaΛઌߦಋೖ͓ͯ͠Γɺͦ͜Ͱͷݟ ϑΟʔυόοΫͯ͠Β͏͜ͱ͕ग़དྷͨ
Slide 24
Slide 24 text
ͳͥRxSwiftͳͷ͔ʁ • એݴతʹهड़Ͱ͖Δ • Rx࣮ރΕٕͨज़Ͱ͋Δ • Rx+αͰඋΘ͍ͬͯΔػೳ܈͕ૉΒ͍͠
Slide 25
Slide 25 text
ͳͥRxSwiftͳͷ͔ʁ • એݴతʹهड़Ͱ͖Δ • Rx࣮ރΕٕͨज़Ͱ͋Δ • Rx+αͰඋΘ͍ͬͯΔػೳ܈͕ૉΒ͍͠
Slide 26
Slide 26 text
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͍ͨ͠ͱ͖ͳͲඇৗʹศ རɻ
Slide 27
Slide 27 text
RxSwiftಠࣗͷ֦ு RxCocoaͷbindingػߏɺDriverͷ֓೦ɺVariableͷ֓೦ͳͲʹ͍ͭ ͯɺଞͷݴޠͷReactive Extensionsͷݟ͕ಘΒΕͳ͍ͷͰ ҙɻRxSwiftͷDocumentΛಡΉɺఆٛՕॴͷίϝϯτΛݟΔͳͲ͠ ͨํ͕Θ͔Γ͍͢ɻ ͨͩɺRx্ͷ֓೦ʹରͯ͠Ͳ͏͍͏ૢ࡞Λͨ͠ΓWrap͢Δ͔ௐΔ ͨΊʹଞͷݴޠͷݟΛੜ͔͢͜ͱͰ͖Δɻ ྫ͑ɺVariableBehaviorSubjectΛWrapͯ͠Δ͕ɺ BehaviorSubjectͷڍಈଞͷݴޠͷRx࣮ͱมΘΒͳ͍ɻ
Slide 28
Slide 28 text
ͱ͍͑ɺ RxSwiftͰϋϚͬͨͱ͜TOP 3
Slide 29
Slide 29 text
ୈ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? ͱݴΘΕͯ?????? …ͦΜͳ࣌ʹಡΜͰྑ͔ͬͨྑࢿྉू
Slide 30
Slide 30 text
ୈ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ͷղઆΛಡΜͰΔͱසൟʹݱΕΔ͕ɺ࠷ॳͦͷ֓೦͕Θ͔Γʹ͘͘ཧղ ʹۤ࿑ͨ͠ɻͷͰɺࢀߟʹͳͬͨࢿྉΛ·ͱΊɻ
Slide 31
Slide 31 text
RxΛ༻͍ͨઃܭʹ͍ͭͯ RxͱClean Architecture
Slide 32
Slide 32 text
ଆͷ֊ɺ֎ଆͷ֊ʹ͍ͭͯؔ৺Λ࣋ͨͳ͍ ྫ͑: EntityUsecase, Presentersionͷ࣮ʹґଘ͠ͳ͍ UsecasePresentersionͷ࣮ʹґଘ͠ͳ͍ ֊͝ͱͷɾґଘؔΛ໌֬ʹ͚Δ͜ͱͰɺ อक͍͢͠TestableͳίʔυΛॻ͘͜ͱ͕ग़དྷ Δɻ Clean Architecture
Slide 33
Slide 33 text
Clean Architecture • Presentation Layer ϏδωεϩδοΫΛ࣮ͨ͠usecaseΛར༻͠ɺͦͷίʔϧόοΫͰมߋΛViewʹඳը͢Δ • Domain Layer ϏδωεϩδοΫΛUsecaseͱ࣮ͯ͢͠ΔɻUsecaseUI֎ଆͷϑϨʔϜϫʔΫʹ͍ͭͯؔ৺Λ࣋ͭඞཁ ͳ͍ɻ·ͨUsecaseʹΑͬͯEntityΛɺViewͰར༻͢ΔͨΊͷModelʹม͢Δ࡞ۀߦ͏ɻ • Data Layer Repository PatternΛར༻ͯ͠ɺσʔλιʔεΛநԽ͢Δɻ֎ͷLayer͔ΒɺͲ͜ʹσʔλ͕อଘ͞ΕͯΔ͔ ҙࣝͤͣѻ͏͜ͱ͕ग़དྷΔɻྫ͑Ωϟογϡ͕͋ΕͦΕΛར༻͢Δɺͳ͚ΕAPI͔ΒσʔλΛҾ͘ɺͱ ͍ͬͨૢ࡞ΛFactoryΛ༻͍ͯநԽ͢ΔɺͳͲɻ
Slide 34
Slide 34 text
͜ΕΛRxͷੈքʹద༻͢Δͱʁ
Slide 35
Slide 35 text
https://github.com/android10/Android- CleanArchitecture Android ͰɺRxΛClean Architecture ʹద༻͠ ͨૉΒ͍͠ݟ͕͋Δ
Slide 36
Slide 36 text
No content
Slide 37
Slide 37 text
No content
Slide 38
Slide 38 text
Presentation Layer • ViewControllerɺը໘ͷϥΠϑαΠΫϧɾભҠΛཧ͢Δͷͱͯ͠ͷΈѻ͏ɻ • Presentation LogicXXXPresenterʹ࣮͢Δɻ (Ex. UserListPresenter) • Presenter = Subscriber PresenterViewΛอ࣋͠ɺUsecase͕ੜͨ͠ϏδωεϩδοΫ(Observable)Λ ߪಡ / Viewʹbind͢Δ • ObservableΛߪಡ/bind͍ͯ͠Εɺࣗಈతʹঢ়ଶͷมԽ͕ө͞ΕΔͷͰϩδο Ϋඇৗʹബ͘ͳΔ • વϏδωεϩδοΫ͕͜ͷʹهड़͞ΕΔ͜ͱͳ͍
Slide 39
Slide 39 text
No content
Slide 40
Slide 40 text
Domain Layer • Usecaseͱͯ͜͜͠ʹϏδωεϩδοΫΛهड़͢Δ (Ex: GetUserListUsecase) • Repository͔ΒObservableʹแ·ΕͨEntity͕ ͬͯ͘Δ(Ex: Observable) • EntityͷObservableΛϏδωεϧʔϧʹԠͯ͡ ༷ʑͳΦϖϨʔλΛར༻ͯ͠Transform͢Δ
Slide 41
Slide 41 text
No content
Slide 42
Slide 42 text
Data Layer • σʔλͷऔಘํ๏(API͔Β/Ωϟογϡ͔Β)ΛFactoryͰநԽ͠ɺඞཁ ͳDetaStoreΛRepositry͕ݺͼग़͢͜ͱʹΑΓಁաతʹѻ͑ΔΑ͏ʹ͢Δ • σʔλͷऔಘϩδοΫObservableͰ͞ΕΔ • ྫ͑UserDataRepositoryɺ UserDataStoreFactoryʹঢ়گʹԠͯ͡ඞཁͳDataStore (CloudUserDataStore/DiscUserDataStore)Λੜͯ͠Β͍ɺ ੜ͞ΕͨStore͔ΒObservableΛऔಘ͢Δ
Slide 43
Slide 43 text
EightͰͷઃܭ • ؙ͝ͱ͜ͷઃܭΛऔΓೖΕͯΔΘ͚Ͱͳ͍ɻඞཁʹԠͯ͡֊Λઃܭɻ • ྫ໊͑ަػೳͰɺΫϥΠΞϯτʹσʔλΛӬଓԽͨ͠ΓΩϟογϡͨ͠Γɺ ͱ͍ͬͨૢ࡞ඞཁͳ͔ͬͨͷͰɺPresenterͱඞཁͳϏδωεϩδοΫʹԠͨ͡ Usecaseʹ͚͍ͯΔ Usecase: EntityΛऔಘՃ͢ΔObservableΛੜ Presenter: Usecase͕ੜͨ͠ObservableΛSuscribe͠ɺ ͖ͬͯͨEntityΛViewModel(DriverVariableΛpropertyͱͯ࣋ͭ͠)ʹม Viewʹbind͢Δɺͱ͍͏Α͏ͳྲྀΕɻ
Slide 44
Slide 44 text
RxSwiftΛ ϓϩμΫτʹಋೖͨ͠ײͳͲ
Slide 45
Slide 45 text
• ֶशίετΓߴ͍ɻ͍ํͱ͍͏ΑΓɺ֓೦తͳͷΛཧղ͢Δͷʹίε τ͕͔͔Δɻ • ίετߴ͍͕ेϖΠ͢Δ(ͱࢥͬͯΔ)ɻঢ়ଶΛγʔέϯεͱͯ͠એݴతʹॻ͚ Δ͜ͱɺͦΕʹରͯ͠Ғ͍ਓ͕ͨͪ࡞ͬͨΦϖϨʔλΛ༻͍ͯૢ࡞͕Ͱ͖Δͷ ඇৗʹศརʢ͜͏͍͏ͷ͋ͬͨΒศརͩͳɺͬͯͷେମطʹ͋Δʣɻͺͬͱݟ ໘ͩͳɺͱ͍͏ॲཧ͕1ߦͱ͔2ߦͰॻ͚Δ͜ͱɻ • (RxʹݶͬͨͰͳ͍͕)σʔλόΠϯσΟϯάUI࣮͕ڪΖָ͘͠ʹͳΔɻ UIͷϩδοΫࣗମബ͘ͳΔͷͰɺঢ়ଶʹؔ͢Δ༷ͷมߋ͕͋ͬͨ߹Model ͚ͩΛमਖ਼͢ΕΑ͘ɺPresentationԿมߋ͢Δඞཁ͕ͳ͍ɻ • ଞͷݴޠͰRxΛ৮Δͱ͖ʹɺֶΜͩࣝΛͦͷ··͑Δͱ͍͏ͷେ͖͍ɻ Androidͷ࣮ͷϓϧϦΫΤετΛݟͯɺԿΛ͍ͯ͠Δ͔ཧղͰ͖ͨΓɺRxʹͭ ͍ͯվΊͯൃݟग़དྷͨΓ͢Δɻ։ൃݴޠOSͷนΛ͑ͨීวతͳࣝͱֶͯ͠ ͿՁ͕͋Δɻ • RxΛֶͿ͜ͱࣗମͬͺΓ৽Ͱָ͍͠ɻ
Slide 46
Slide 46 text
$PQZSJHIU4BOTBO *OD"MMSJHIUTSFTFSWFE 4BOTBOҰॹʹ৽͍͠ՁΛ࡞͍ͬͯ͘ ؒΛ͕͍ͯ͞͠·͢ɻ 3VCZ 3VCZPO3BJMT ʢ8FCΞϓϦέʔγϣϯʣ $ɼ"41/&5.7$ ʢ8FCΞϓϦέʔγϣϯʣ J04"OESPJEΞϓϦ ݸਓ໊͚ཧΞϓϦʮ&JHIUʯ ໊σʔλԽࢄॲཧγεςϜ ๏ਓ໊͚ཧαʔϏεʮ4BOTBOʯ ๏ਓ໊͚ཧαʔϏε ʮ4BOTBOʯ ݸਓ໊͚ཧΞϓϦʮ&JHIUʯ ΤϯδχΞืूத 4BOTBO࠾༻ ݕࡧ SFDSVJU!TBOTBODPN·Ͱ ͓ؾܰʹ͝࿈བྷ͍ͩ͘͞ɻ ڵຯͷ͋Δํ
Slide 47
Slide 47 text
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠