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.9k
いまさら聞けないRxSwift入門
モバイルメソッド大阪第一回 RxSwift入門資料です。
M.Inomata
May 10, 2018
Tweet
Share
More Decks by M.Inomata
See All by M.Inomata
20221005_AI勉強会
ecoopnet
0
510
20220112_AI勉強会
ecoopnet
0
520
AI最新論文読み会2021年11月
ecoopnet
0
330
AI最新論文読み会2021年5月
ecoopnet
0
520
AI最新論文読み会2020年12月
ecoopnet
0
390
AI最新技術Update会 8月
ecoopnet
0
450
医療ディープラーニング勉強会 DL勉強会 第3回 2020.4
ecoopnet
0
490
Kotlin Coroutines ことはじめ
ecoopnet
2
310
ハマった時に役立つ通信系デバッグの話
ecoopnet
3
1.7k
Other Decks in Programming
See All in Programming
AI Agents with JavaScript
slobodan
0
130
Compose Navigation実装の見通しを良くする
hiroaki404
0
180
PHPer's Guide to Daemon Crafting Taming and Summoning
uzulla
2
1.1k
Denoでフロントエンド開発 2025年春版 / Frontend Development with Deno (Spring 2025)
petamoriken
1
1.3k
JavaOne 2025: Advancing Java Profiling
jbachorik
1
320
体得しよう!RSA暗号の原理と解読
laysakura
3
540
OpenTelemetryを活用したObservability入門 / Introduction to Observability with OpenTelemetry
seike460
PRO
0
340
なぜselectはselectではないのか
taiyow
2
310
SLI/SLOの設定を進めるその前に アラート品質の改善に取り組んだ話
tanden
2
730
The Evolution of Enterprise Java with Jakarta EE 11 and Beyond
ivargrimstad
0
970
複数ドメインに散らばってしまった画像…! 運用中のPHPアプリに後からCDNを導入する…!
suguruooki
0
430
PHPUnit 高速化テクニック / PHPUnit Speedup Techniques
pinkumohikan
1
1.2k
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
30k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
135
33k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
177
52k
Scaling GitHub
holman
459
140k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
227
22k
Gamification - CAS2011
davidbonilla
81
5.2k
Fireside Chat
paigeccino
37
3.3k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.5k
Typedesign – Prime Four
hannesfritz
41
2.6k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Visualization
eitanlees
146
16k
Optimizing for Happiness
mojombo
377
70k
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 “” }) Λ·ͱΊͯͬͯ͘ΕΔΑ͏ʹͳΓ·͢ɻ ˎ