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.7k
いまさら聞けないRxSwift入門
モバイルメソッド大阪第一回 RxSwift入門資料です。
M.Inomata
May 10, 2018
Tweet
Share
More Decks by M.Inomata
See All by M.Inomata
20221005_AI勉強会
ecoopnet
0
480
20220112_AI勉強会
ecoopnet
0
490
AI最新論文読み会2021年11月
ecoopnet
0
300
AI最新論文読み会2021年5月
ecoopnet
0
490
AI最新論文読み会2020年12月
ecoopnet
0
360
AI最新技術Update会 8月
ecoopnet
0
420
医療ディープラーニング勉強会 DL勉強会 第3回 2020.4
ecoopnet
0
460
Kotlin Coroutines ことはじめ
ecoopnet
2
280
ハマった時に役立つ通信系デバッグの話
ecoopnet
3
1.6k
Other Decks in Programming
See All in Programming
CSC509 Lecture 09
javiergs
PRO
0
100
Synchronizationを支える技術
s_shimotori
1
150
讓數據說話:用 Python、Prometheus 和 Grafana 講故事
eddie
0
350
外部システム連携先が10を超えるシステムでのアーキテクチャ設計・実装事例
kiwasaki
1
220
開発効率向上のためのリファクタリングの一歩目の選択肢 ~コード分割~ / JJUG CCC 2024 Fall
ryounasso
0
360
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
210
Honoの来た道とこれから
yusukebe
19
3k
プロジェクト新規参入者のリードタイム短縮の観点から見る、品質の高いコードとアーキテクチャを保つメリット
d_endo
1
1k
Pinia Colada が実現するスマートな非同期処理
naokihaba
2
150
生成 AI を活用した toitta 切片分類機能の裏側 / Inside toitta's AI-Based Factoid Clustering
pokutuna
0
570
Android 15 でアクションバー表示時にステータスバーが白くなってしまう問題
tonionagauzzi
0
140
Jakarta Concurrencyによる並行処理プログラミングの始め方 (JJUG CCC 2024 Fall)
tnagao7
1
230
Featured
See All Featured
Teambox: Starting and Learning
jrom
132
8.7k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
StorybookのUI Testing Handbookを読んだ
zakiyama
26
5.2k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Bash Introduction
62gerente
608
210k
A better future with KSS
kneath
238
17k
Music & Morning Musume
bryan
46
6.1k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
107
49k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
Practical Orchestrator
shlominoach
186
10k
The Cost Of JavaScript in 2023
addyosmani
45
6.6k
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 “” }) Λ·ͱΊͯͬͯ͘ΕΔΑ͏ʹͳΓ·͢ɻ ˎ