Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
いまさら聞けないRxSwift入門
Search
M.Inomata
May 10, 2018
Programming
15
3.8k
いまさら聞けないRxSwift入門
モバイルメソッド大阪第一回 RxSwift入門資料です。
M.Inomata
May 10, 2018
Tweet
Share
More Decks by M.Inomata
See All by M.Inomata
20221005_AI勉強会
ecoopnet
0
490
20220112_AI勉強会
ecoopnet
0
500
AI最新論文読み会2021年11月
ecoopnet
0
310
AI最新論文読み会2021年5月
ecoopnet
0
500
AI最新論文読み会2020年12月
ecoopnet
0
370
AI最新技術Update会 8月
ecoopnet
0
430
医療ディープラーニング勉強会 DL勉強会 第3回 2020.4
ecoopnet
0
470
Kotlin Coroutines ことはじめ
ecoopnet
2
290
ハマった時に役立つ通信系デバッグの話
ecoopnet
3
1.6k
Other Decks in Programming
See All in Programming
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
230
CSC509 Lecture 14
javiergs
PRO
0
140
CSC305 Lecture 25
javiergs
PRO
0
130
nekko cloudにおけるProxmox VE利用事例
irumaru
3
420
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
MCP with Cloudflare Workers
yusukebe
2
220
プロダクトの品質に コミットする / Commit to Product Quality
pekepek
2
770
17年周年のWebアプリケーションにTanStack Queryを導入する / Implementing TanStack Query in a 17th Anniversary Web Application
saitolume
0
250
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
300
命名をリントする
chiroruxx
1
390
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
700
暇に任せてProxmoxコンソール 作ってみました
karugamo
1
720
Featured
See All Featured
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.3k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Become a Pro
speakerdeck
PRO
26
5k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
Making the Leap to Tech Lead
cromwellryan
133
9k
The Language of Interfaces
destraynor
154
24k
Optimising Largest Contentful Paint
csswizardry
33
3k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
510
Transcript
͍·͞Βฉ͚ͳ͍ RxSwiftೖ גࣜձࣾ tech vein ழມ ॆԝ
ࣗݾհ גࣜձࣾ tech vein ද ழມ ॆԝ (͍ͷ·ͨ ΈͭͻΖ) ձࣾHP:
https://www.tech-vein.com/ private twitter: @ino2222
ެ։தͷAndroid/iOSΞϓϦ ιϧΞϯϦʔγϡ ࣗͷΦϦΩϟϥΛ࡞ΔεϚϗ31( IUUQTPVOUFDIWFJODPN IUUQTCJUMZ);BV3 $IJJ2 ҬͰॿ͚߹͑Δಗ໊૬ஊΞϓϦ IUUQTBQQMFDP,[IYD ϙέοτΞϒετϥΫτ ҩྍैࣄऀ͚ͷ࠷৽δϟʔφϧӾཡΞϓϦ
ࠓ͓͢Δ͜ͱ • RxSwiftͱ • RxSwiftͷجຊ • ͜Ε͚֮ͩ͑ͨΒ͑ΔΑ͏ʹͳΔ͔ʁ
ࠓ͓͠ͳ͍͜ͱ(ͬͯΔਓ͚) • Cold -> Hot ม • Reactive Streams(backPressure ͳͲ)
RxSwiftͱ • ֤ݴޠͰ࣮͞Ε͍ͯΔʮRx(Reactive Extension)ʯ ͷSwift͚ϥΠϒϥϦ • Reactive Extension … ͱͱ
C#.NET ͰΘΕͩͨ͠ Rx.NET ͕ΦϦδφϧ • Java(RxJava), JavaScript(RxJS), C++(RxCpp), Ruby(Rx.rb), PHP(RxPHP) ͳͲͳͲଟ
ReactiveXͱԿ͔ʁ ReactiveX = Reactive Extension Reactive Programming ʹԠతϓϩάϥϛϯάΛ͢Δͨ Ίͷ Extension
= ֦ு ReactiveX - σβΠϯύλʔϯͷ Observable ύλʔϯ - σβΠϯύλʔϯͷ Iterator ύλʔϯ - ؔܕϓϩάϥϛϯά ΛΈ߹Θͤͯ࡞ΒΕͨϥΠϒϥϦͷ͜ͱ
Observerύλʔϯ σʔλΛࢹ͢Δਓͱ͑ΔΈΛ நԽͨ͠σβΠϯύλʔϯ ͕͜͜େࣄʂ
Iterator ύλʔϯ https://ja.wikipedia.org/wiki/Iterator_ύλʔϯ Ұ࿈ͷσʔλΛॱ൪ʹॲཧ͢ΔσβΠϯύλʔϯ ͕͜͜େࣄʂ
ؔܕϓϩάϥϛϯά ˠ ͦΕͧΕͷؔ */065ʹ͔͠ Өڹ͠ͳ͍ ʹ෭࡞༻͕ͳ͍
ͦͯ͠Rx…
͕͜͜خ͍͠RxSwift ࣌ܥྻσʔλΛؚΉɺ͋ΒΏΔσʔλΛετϦʔϜͱͯ͠εϚʔτʹॲཧͰ͖Δɻ ετϦʔϜͱͯ͠ॱ൪ʹॲཧͰ͖Δ͜ͱͰɺ͍͜͠ඇಉظ੍ޚ͕γϯϓϧʹͳΔɻ →ίʔϧόοΫࠈ͔Βͷղ์ நԽʹΑΓૹΓखɾड͚औΓखΛΒͳ͍··ॲཧ͕ग़དྷΔͷͰૄ݁߹ʹͳΔɻ →վมͷӨڹൣғ͕খ͘͞ͳΔ →෦Խɾڞ௨Խ͕ਐΉ ֤छετϦʔϜΛѻ͏ศརͳಓ۩͕ἧ͍ͬͯΔɻ →ετϦʔϜͷੜɾมɾ߹Λߦ͏ΦϖϨʔλ܈ →ετϦʔϜͷεϨουΓସ͑ػೳ
RxSwiftͷجຊ
RxSwiftͷجຊ ͜Ε͚ͩͬͯͨΒ RxŧŔŕŪɹɹ ũƄŝſͬͯݴ͑Δ
RxSwiftͷجຊ ͜Ε͚ͩͬͯͨΒ RxŧŔŕŪ(ŦƄş)ũƄŝſͬͯݴ͑Δ͔ʁ
Observable, Subject, Disposable ΩϞʹͳΔͷɺ ͜ͷ̏ܥ౷ͷΫϥε(ϓϩτίϧ)ɻ Ϋϥε৭ʑ͋Γ·͕͢ɺ ͍͍ͩͨ͜ΕΒͷؒͰ͢ɻ
+ subscribe() Observable + onNext() + onError() + onCompleted() +
subscribe() Subject ܧঝ + dispose() Disposable subscribe() ͷΓ Observable, Subject, Disposable
Observable<T> ༁͢Δͱɺࢹ͢Δ͜ͱ͕Ͱ͖Δରɻ ҙͷܕ T ʹ͍ͭͯɺࢹͯ͠σʔλʢΠϕϯτ)Λड͚औ Δࣄ͕Ͱ͖ΔΫϥεɾΠϯελϯεͰ͢ɻ ࣌ܥྻͰσʔλ͕ObservableʹྲྀΕΔ͜ͱ͔Βɺ ObservableετϦʔϜ(Stream)ͱݺͼ·͢ɻ + subscribe()
Observable + onNext() + onError() + onCompleted() + subscribe() Subject ܧঝ + dispose() Disposable subscribe() ͷΓ
“Hello” “Rx” “World” Observable<String> completed ࣌ؒͷྲྀΕ ࣮ߦ݁ՌɿԼͷॱʹग़ྗ͞ΕΔɻ OFYU )FMMP OFYU
3Y OFYU 8PSME DPNQMFUFE
Subject ؆୯ʹݴ͏ͱɺҙͷσʔλΛྲྀ͢ػೳ(Observer ͱͯ͠ͷػೳ)Λ࣋ͬͨObservable࣮ɻ RxSwift ͷSubject PublishSubject, BehaviorSubject, ReplaySubject ͷ̏छྨɻ +
subscribe() Observable + onNext() + onError() + onCompleted() + subscribe() Subject ܧঝ + dispose() Disposable subscribe() ͷΓ
SubjectObserverΛܧঝ͍ͯ͠ΔͷͰ Observableͱͯ͠ѻ͑Δɻ SubjectonNext(), onComplete(), onError() ͳͲͷσʔλɾΠϕϯτൃ৴ϝιουΛ࣋ͭɻ
ετϦʔϜͷΠϕϯτ • next σʔλ͕ਖ਼͘͠ྲྀΕ͖ͯͨͱ͍͏ΠϕϯτɻҙͷܕͷσʔλΛҾʹ࣋ ͭɻ • error ετϦʔϜத͕Τϥʔ(ྫ֎)Ͱҟৗఀࢭͨ࣌͠ʹྲྀΕΔΠϕϯτɻൃੜͨ͠ ΤϥʔΛҾʹ࣋ͭɻ •
completed ετϦʔϜ͕ਖ਼ྃͨ࣌͘͠͠ʹൃੜ͢ΔΠϕϯτɻετϦʔϜͷ࠷ޙʹ Ұ͚ͩൃੜ͢Δɻ ʢೖྗΛͪଓ͚͍ͨͱ͖completedΛྲྀ͞ͳ͍ʣ
ετϦʔϜͷΠϕϯτ next completed next next next next next completed next
next ετϦʔϜ next ͰσʔλΛྲྀ͠ɺDPNQMFUFEͰऴྃ͢Δ DPNQMFUFE͕དྷͨΒɺͦͷઌOFYUFSSPSྲྀͤͳ͍ next next next FSSPS ྫ֎ ͕དྷͨΒɺͦͷઌOFYUDPNQMFUFEFSSPSྲྀͤͳ͍ error
RxSwift ͷ Subject͍͚ ɾPublishSubject ɹաڈͷσʔλΛอ࣋͠ͳ͍Subjectɻ ɹར༻ྫɿϘλϯͷλοϓΠϕϯτ ɾBehaviorSubject ɹաڈͷσʔλͷ͏ͪۙͷ̍ͭΛอ͍࣋ͯ͠ΔSubjectɻ ɹར༻ྫɿεΠονON/OFFঢ়ଶɾϢʔβεςʔλεͳͲ ɾReplaySubject.create(bufferSize:
Int) ɹաڈͷۙσʔλΛࢦఆόοϑΝʹೖΔ͚ͩอ͍࣋ͯ͠ΔSubjectɻ ɹόοϑΝαΠζ̌ͳΒPublishSubjectͱಉ͡ɻ ɹόοϑΝαΠζ̍ͳΒBehaviorSubjectͱಉ͡ɻ ɹར༻ྫɿࢥ͍͔ͭͳ͍…ཤྺܥͰ͑Δ͔ʁ
PublishSubject PublishSubject<T> completed ࣌ؒͷྲྀΕ TVCTDSJCF TVCTDSJCF
BehaviorSubject BehaviorSubject<T> completed ࣌ؒͷྲྀΕ TVCTDSJCF TVCTDSJCF TVCTDSJCF
ReplaySubject ReplaySubject<T>.create(bufferSize: 2) completed ࣌ؒͷྲྀΕ TVCTDSJCF TVCTDSJCF TVCTDSJCF
Disposable ༁͢ΔͱɺഇغɾॲͰ͖Δͷɻ ObservableΛ subscribe(ߪಡ) ͢ΔͱΓͱͯ͠ Disposable Λ ฦ͠·͢ɻ Disposableʹରͯ͠dispose()ΛݺͿ͜ͱͰɺඇಉظॲཧͷΩϟϯη ϧͳͲɺࢹ͢Δଆͱͯ͠ॲཧΛࢭΊΔࣄ͕Ͱ͖·͢ɻ
ྫɿը໘Λดͨ͡Β௨৴Ωϟϯηϧͨ͠ΓɺೖྗͪΛࢭΊͨΓ͢ Δɻ + subscribe() Observable + onNext() + onError() + onCompleted() + subscribe() Subject ܧঝ + dispose() Disposable subscribe() ͷΓ
+ subscribe() Observable + onNext() + onError() + onCompleted() +
subscribe() Subject ܧঝ + dispose() Disposable subscribe() ͷΓ ॲཧͷྲྀΕ
+ subscribe() Observable + onNext() + onError() + onCompleted() +
subscribe() Subject ܧঝ + dispose() Disposable subscribe() ͷΓ subscribe(onNext: … ) ॲཧͷྲྀΕ
+ subscribe() Observable + onNext() + onError() + onCompleted() +
subscribe() Subject ܧঝ + dispose() Disposable subscribe() ͷΓ subscribe(onNext: … ) ΠϕϯτΛҾͷΫϩʔδϟͰͪड͚Δ PO/FYU lIFMMPz PO/FYU l3Yz PO/FYU l8PSMEz ʜ PO$PNQMFUFE ॲཧͷྲྀΕ
+ subscribe() Observable + onNext() + onError() + onCompleted() +
subscribe() Subject ܧঝ + dispose() Disposable subscribe() ͷΓ subscribe(onNext: … ) dispose() ΠϕϯτΛҾͷΫϩʔδϟͰͪड͚Δ ॲཧͷྲྀΕ
+ subscribe() Observable + onNext() + onError() + onCompleted() +
subscribe() Subject ܧঝ + dispose() Disposable subscribe() ͷΓ subscribe(onNext: … ) dispose() ΠϕϯτΛҾͷΫϩʔδϟͰͪड͚Δ ॲཧΛࢭΊͨ͘ͳͬͨΒEJTQPTFΛݺͿ ॲཧͷྲྀΕ
(x, y) (x2, y2) (x3, y3) ྫɿλονΠϕϯτͷࢹ Observable<Point>) ࣌ؒͷྲྀΕ (x4,
y4) TVCTDSJCF EJTQPTF EJTQPTFE (x2, y2) (x3, y3) Disposable.dispose()
DisposeBag disposeΛ͍͍ͪͪ͢ΔͷΊΜͲ͍͘͞ɻ ͦΜͳͱ͖DisposeBagɻ
DisposeBag ը໘ͳͲɺࢹݩΠϯελϯε͕ࢮΜͩΒࣗಈͰͥΜ Ϳdisposeͯ͠΄͍࣌͠ʹ͑ΔศརΫϥεɻ DisposeBagΠϯελϯεʹಥͬࠐΜͰ͓͘ͱɺ disposeBag͕ղ์͞ΕΔ࣌ʹ·ͱΊͯdisposeBagͷ தͷdisposableΛdisposeͯ͘͠ΕΔɻ ˠ3Yඪ४Ͱͳ͍Ͱ͕͢ΊͪΌͪ͘Ό͍·͢ɻ ɹͱΓ͋͑ͣ%JTQPTF#BH࡞ͬͯ์ΓࠐΜͰ͓͚͍͍ͱࢥ͏ɻ
(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͢Δɻ
IUUQTHJUIVCDPNFDPPQOFUSYTXJGUCFHJOOFSCMPCNBTUFS3Y4XJGU4BNQMF3Y4XJGU4BNQMF 6*4BNQMF7JFX$POUSPMMFSTXJGU DisposeBagͷར༻ྫ ˡ6*7JFX$POUSPMMFSͷ ϑΟʔϧυʹEJTQPTF#BHΛஔ͍͓ͯ͘ ˠը໘͕ดͨ͡Β6*7JFX$POUSPMMFS͕ࢮ͵ɻ ˠ6*7JFX$POUSPMMFS͕ࢮΜͩΒ%JTQPTF#BHࢮ͵ɻ ˠ%JTQPTF#BH͕ࢮΜͩΒඥͮ͘EJTQPTBCMF͕ ɹEJTQPTF ͞ΕΔɻ
ΦϖϨʔλ(Operator) Rxʹ Observable, SubjectͰ࡞ͬͨετϦʔϜΛૢ ࡞͢Δػೳ͕ఏڙ͞Ε͍ͯ·͢ɻ ετϦʔϜ(=Observable)Λมɾ߹ɾੜ͢Δૢ ࡞ͷ͜ͱΛʮΦϖϨʔλʯͱݺͼ·͢ɻ
ετϦʔϜΛ࡞Δ ̍ … Subject Ϋϥε͔Β࡞Δ ̎ … from(), of(), just()
ͳͲͰҙͷྻɾΛ มͯ͠࡞Δ ̏ … Observable.create() Ͱҙͷॲཧ͔Β࡞Δ
PublishSubject<T> completed ࣌ؒͷྲྀΕ TVCTDSJCF TVCTDSJCF
10 Observable.just(10) completed σʔλετϦʔϜͷྲྀΕ just( ) ετϦʔϜม
“a” Observable.of(“a”, ”b”, ’c”, ”d”, “e”) completed σʔλετϦʔϜͷྲྀΕ of( ,
, , , ) ετϦʔϜม “b” “c” “d” “e”
1 Observable.from([1, 3, 5, 7, 9]) completed σʔλετϦʔϜͷྲྀΕ from([ ,
, , , ]) ετϦʔϜม 3 5 7 9
Observable.error(MyError()) σʔλετϦʔϜͷྲྀΕ error( ) ετϦʔϜม myError
1 Observable.create(ҙͷॲཧ) completed σʔλετϦʔϜͷྲྀΕ create(ҙͷॲཧ) ετϦʔϜม 3 5 7 9
map: ετϦʔϜΛม͢Δ map(_:((T) -> R)) -> Observable<R> ҙͷܕɾͷετϦʔϜΛՃͯ͠ผͷܕɾͷετϦʔ Ϝʹม͠·͢ɻ
filter: ετϦʔϜΛϑΟϧλ͢Δ filter(_:((T) -> Bool)) -> Observable<T> ྲྀΕ͖ͯͨετϦʔϜΛҾͷؔͰॱ൪ʹॲཧ͠ɺ ݁Ռ͕true ʹͳΔσʔλ͚ͩͷετϦʔϜʹม͠·͢ɻ
flatMap: ετϦʔϜΛม͢ Δ flatMap(_:((T) -> Observable<R>)) -> Observable<R> ҙͷܕɾͷετϦʔϜΛՃͯ͠ผͷܕɾͷετϦʔ Ϝʹม͠·͢ɻmapͱ΄΅ಉ͡Ͱ͕͢ɺΫϩʔδϟͷฦΓ
͕Observableʹͳ͍ͬͯΔ͕ҟͳΓ·͢ɻ
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
filter, map, flatMap ͷྫ
map ͱ flatMap ͷ͍͚ map … ಉظతʹॲཧͰ͖Δσʔλมʹ͍·͢ɻ ྫɿStringܕ -> URLܕɺInt
ܕΛॱ൪ʹܭࢉͯ͠ม͢Δ flatMap … ඇಉظॲཧͳͲɺผͷετϦʔϜΛͬͯม͢Δ ࣌ʹ͍·͢ɻ ྫɿtwitterͷπΠʔτIDετϦʔϜΛflatMapͯ͠ɺॱ൪ʹAPI Λୟ͍ͯ༰Λऔಘͯ͠λΠϜϥΠϯදࣔ͢Δ
map ͱ subscribe ͷ͍͚ ʢ؆୯ͳઆ໌ʣ ͲͪΒͰonNext ʹ͍ͭͯॲཧ͕ग़དྷ·͕͢ɺ subscribe()ܨ͍ͩετϦʔϜΛಈ͔࢝͠ΊΔͱ͍͏େࣄͳׂ͕͋Γ·͢ɻ (ετϦʔϜมͷऴ) subscribe()
ετϦʔϜมͰͳ͘ετϦʔϜ࣮ߦʹͳΔͨΊɺObservable Ͱͳ͘DisposableΛฦ͠·͢ɻ͜ͷͨΊmapͱҧ͍͏͠ΖʹετϦʔϜΛͭͳ ͛Δ͜ͱ͕Ͱ͖·ͤΜɻ
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”
ଞʹ ศརͳΦϖϨʔλ͕ ͨ͘͞Μ͋Γ·͢ ࢀߟɿ http://reactivex.io/documentation/operators.html http://rxmarbles.com/
ศརͳΦϖϨʔλͷҰྫ • zip, merge, combineLatest … ෳͷετϦʔϜΛฒྻʹಈ͔ͯ̍ͭ͠ͷετϦʔϜʹ·ͱΊΔɻ • catchError
… Τϥʔ͕ൃੜͨ͠ͱ͖ʹϦΧόϦॲཧΛͯ͠onNext ʹม͢Δɻ࠶ΤϥʔΛ͛Δ(throw)͜ͱͰɺผͷΤ ϥʔʹม͢Δ͜ͱͰ͖Δ • distinctUntilChanged … ॏෳআ֎ɻετϦʔϜதʹมߋ͕͋ͬͨͱ͖͚ͩ௨͢Δ • throttle, debounce … ࿈ଧࢭʹ͑ΔϑΟϧλɻσʔλΛड͚औͬͨΒҰఆ࣌ؒؒΛ։͚ͳ͍ͱ͕࣍ൃՐ͠ͳͨ͘͠Γ͢Δ • toArray … ྻʹ·ͱΊΔɻcompleted·Ͱͬͯ̍ͭͷྻʹม͢Δ • groupBy … ετϦʔϜͷσʔλΛ͖ͳ݅ͰάϧʔϓԽ͢Δ
Try Rx!
RxSwift/RxCocoa ͷ Πϯετʔϧ ᶃ CocoaPods → Podfile: ᶄ pod install
ᶅ .swift ϑΝΠϧͰimport ※Carthage / Swift Package Manager ʹରԠͯ͠·͢ɻ https://github.com/ReactiveX/RxSwift 3Y$PDPBʹ͍ͭͯޙड़ ·ͨ
Hello, Rx world ˠ
Hello, Rx world2 ˠ
Hello, Rx world3 ˣ
RxCocoa ʹ͍ͭͯ RxSwift ͷαϒηοτతͳϥΠϒϥϦͰ͢ɻ RxSwift ͕७ਮͳ Rx ࣮ͰɺRxCocoa iOS/Mac։ൃʹศརͳଐΫϥεɾ֦ு
Λఏڙ͍ͯ͠·͢ɻ RxCocoa Λ͏ʹ RxSwift ͱผʹϥΠϒϥϦΠϯετʔϧ͕ඞཁʹͳΓ·͢ɻ > pod ‘RxCocoa' Ͱ pod install ͯ͠ɺswiftίʔυͰ import RxCocoa Λॻ͍͓ͯ͘ͱ͑·͢ɻ
RxCocoa ͕ఏڙ͢Δػೳͷྫ
UIKit ఏڙΫϥεͷ rx Խ UIView, URLSession ͳͲUIKit,͕ఏڙ͢ΔΫϥεͷϓϩύςΟɾΠϕϯτ ΛrxԽͯ͠ɺObservableΠϯελϯεͱͷ૬ޓόΠϯυΛՄೳʹ͢Δɻ ྫɿ -
ࣈΛςΩετೖྗͨ͠Βϥϕϧʹܭࢉ݁ՌΛදࣔ͢Δɻ - ϘλϯΛԡͨ͠ΒAPI௨৴ͯ͠ɺAPI࣮ߦ݁ՌΛϥϕϧʹදࣔ͢Δɻ - URLSession ͷAPI௨৴݁ՌJSONΛܗͯ͠ϓϩϑΟʔϧը໘ʹදࣔ
͓ͬͯ͘ͱྑ͍͔ɻ • RxAlamofire ͳͲ୳ͤઌਓ͕RxԽͯ͘͠ ΕͯΔͷɺެ։͞ΕͯΔίʔυ͕݁ߏ͋ Γ·͢ɻ
·ͱΊ
·ͱΊ • σʔλͷΓऔΓΛετϦʔϜͱͯ͠ߟ͑Α͏ • ͱΓ͋͑ͣRxͷετϦʔϜ(Observable)ʹͰ͖ͨ Βͬͪ͜ͷͷɻͲ͏Ճͯ͠తͷܗࣜʹ͢Δ ͔Λߟ͑Α͏ɻ • RxΛۃΊͨΒϩδοΫ͕γϯϓϧʹ෭࡞༻ͳ͘ɺ εϚʔτʹॻ͚ΔΑ͏ʹͳΔɻඇಉظॲཧා͘
ͳ͍ʢͣʣ
͍͞͝ʹ • tech vein Ͱ͜ΜͳΞϓϦΤϯδχΞɺ ϑϦʔϥϯεɾύʔτφʔ༷Λืू͍ͯ͠·͢ɻ • RxSwift, RxJava Λ͍͍ͨ
• MVVMɺ CleanArchitectureɺ DI Λ׆༻ͨ͠ઃܭʹڵຯ͕͋Δ ڵຯ͕͋ΔํɺͪΐͬͱΛฉ͍ͯΈ͍ͨํ ͨͩ3YͷΛͬͱฉ͖͍ͨํ ͥͻ͓͕͚Լ͍͞ʂ
ࢀߟ • ࠓճͷαϯϓϧίʔυஔ͖ɹʴ RxCocoa Λͬͨ؆୯ͳΞϓ Ϧ • GitHub: https://github.com/ecoopnet/rxswift-beginner •
ࢀߟهࣄ • Կͱͳ͘RxJavaΛͬͯΔ͚Ͳਖ਼Α͔ͬͯ͘ͳ͍ਓ͕ಡ Ήͱྑͦ͞͏ͳهࣄɾجૅฤ https://qiita.com/k-mats/items/4d374460a3f6284dd09f
͕࣌ؒ͋ΕαϯϓϧΞϓϦσϞ
͝ਗ਼ௌ ͋Γ͕ͱ͏͍͟͝·ͨ͠
Driver ͷ͍ํ Observable ͱ΄΅ಉ͡
Driver ͷ͍ํ Observable ͱ΄΅ಉ͡ʁ →Τϥʔॲཧ͕ཁΒͳ͍ͷࡉ͔͍ҧ͍͋Δ
Driver (ิ) ۩ମతʹ observable.asDriver(onErrorJustReturn: “”) ͱ͢Δͱ - UIεϨουͰॲཧɿ .observeOn(MainScheduler.instance) -
Cold -> Hot มɿ .share(replay:1) - Τϥʔ࣌ʹࢹ͕ࢭ·ΔͷΛ͙ͨΊͷϦΧόϦॲཧɿ .catchError({ _ in “” }) Λ·ͱΊͯͬͯ͘ΕΔΑ͏ʹͳΓ·͢ɻ ˎ