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
クリエイティブコーディングとRuby学習 / Creative Coding and Learning Ruby
chobishiba
0
3.9k
テスト自動化失敗から再挑戦しチームにオーナーシップを委譲した話/STAC2024 macho
ma_cho29
1
1.3k
103 Early Hints
sugi_0000
1
230
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~ / WebSocket Server in PHP - To know reactive applications
seike460
PRO
2
350
DevFest Tokyo 2025 - Flutter のアプリアーキテクチャ現在地点
wasabeef
5
910
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
220
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
130
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
3
470
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
470
선언형 UI에서의 상태관리
l2hyunwoo
0
160
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
130
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
130
Featured
See All Featured
How to train your dragon (web standard)
notwaldorf
88
5.7k
The Cult of Friendly URLs
andyhume
78
6.1k
Site-Speed That Sticks
csswizardry
2
190
Designing for humans not robots
tammielis
250
25k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
170
Reflections from 52 weeks, 52 projects
jeffersonlam
347
20k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
Writing Fast Ruby
sferik
628
61k
Being A Developer After 40
akosma
87
590k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
520
Agile that works and the tools we love
rasmusluckow
328
21k
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 “” }) Λ·ͱΊͯͬͯ͘ΕΔΑ͏ʹͳΓ·͢ɻ ˎ