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

$PQZSJHIU˜4BOTBO *OD"MMSJHIUTSFTFSWFE

Slide 5

Slide 5 text

$PQZSJHIU˜4BOTBO *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ͷྺ࢙ • 2009೥11݄ ϚΠΫϩιϑτ͕.Net Framework ্ͷϥΠϒϥϦͱ࣮ͯ͠ݧతʹެ։ • 2011೥11݄ ਖ਼ࣜͳ੡඼ͱͯ͠ঢ֨ɺ.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͢Δ͔ௐ΂Δ ͨΊʹଞͷݴޠͷ஌ݟΛੜ͔͢͜ͱ͸Ͱ͖Δɻ
 
 ྫ͑͹ɺVariable͸BehaviorSubjectΛ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

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

Slide 33

Slide 33 text

Clean Architecture • Presentation Layer
 ϏδωεϩδοΫΛ࣮૷ͨ͠usecaseΛར༻͠ɺͦͷίʔϧόοΫͰมߋΛViewʹඳը͢Δ • Domain Layer
 ϏδωεϩδοΫΛUsecaseͱ࣮ͯ͠૷͢ΔɻUsecase͸UI΍֎ଆͷϑϨʔϜϫʔΫʹ͍ͭͯؔ৺Λ࣋ͭඞཁ͸ ͳ͍ɻ·ͨ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 Logic͸XXXPresenterʹ࣮૷͢Δɻ
 (Ex. UserListPresenter) • Presenter = Subscriber
 Presenter͸ViewΛอ࣋͠ɺ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(Driver΍VariableΛ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

$PQZSJHIU˜4BOTBO *OD"MMSJHIUTSFTFSWFE 4BOTBO͸Ұॹʹ৽͍͠Ձ஋Λ࡞͍ͬͯ͘ ஥ؒΛ͕͍ͯ͞͠·͢ɻ 3VCZ 3VCZPO3BJMT ʢ8FCΞϓϦέʔγϣϯʣ $ɼ"41/&5.7$ ʢ8FCΞϓϦέʔγϣϯʣ J04"OESPJEΞϓϦ   ݸਓ޲໊͚ࢗ؅ཧΞϓϦʮ&JHIUʯ   ໊ࢗσʔλԽ෼ࢄॲཧγεςϜ   ๏ਓ޲໊͚ࢗ؅ཧαʔϏεʮ4BOTBOʯ   ๏ਓ޲໊͚ࢗ؅ཧαʔϏε ʮ4BOTBOʯ   ݸਓ޲໊͚ࢗ؅ཧΞϓϦʮ&JHIUʯ ΤϯδχΞืूத 4BOTBO࠾༻ ݕࡧ SFDSVJU!TBOTBODPN·Ͱ ͓ؾܰʹ͝࿈བྷ͍ͩ͘͞ɻ ڵຯͷ͋Δํ͸

Slide 47

Slide 47 text

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