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のobserveOnとsubscribeOnを理解する
Search
Shinichiro Oba
April 14, 2016
Programming
20
7.2k
RxSwiftのobserveOnとsubscribeOnを理解する
RxSwift勉強会 @ Sansanの発表資料です
http://connpass.com/event/27933/
Shinichiro Oba
April 14, 2016
Tweet
Share
More Decks by Shinichiro Oba
See All by Shinichiro Oba
サーバサイドエンジニアと効率よく開発するためにやっていること
bricklife
8
2.1k
アッテ開発の技術:Swift と RxSwift
bricklife
19
17k
Swift 2.0でRxSwift、ReactKit、ReactiveCocoaを使ってみた
bricklife
10
4.8k
ReactiveCocoaのゆるい紹介とメルカリでの活用事例
bricklife
3
3.6k
Apple Pay対応のやりかた
bricklife
1
25k
iOS版グローバル対応の罠と技
bricklife
8
24k
Other Decks in Programming
See All in Programming
Remix on Hono on Cloudflare Workers
yusukebe
1
280
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
600
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
540
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.3k
イベント駆動で成長して委員会
happymana
1
320
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
110
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
880
Realtime API 入門
riofujimon
0
150
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
100
Ethereum_.pdf
nekomatu
0
460
Featured
See All Featured
Practical Orchestrator
shlominoach
186
10k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Thoughts on Productivity
jonyablonski
67
4.3k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Adopting Sorbet at Scale
ufuk
73
9.1k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
Docker and Python
trallard
40
3.1k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
A Modern Web Designer's Workflow
chriscoyier
693
190k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
Music & Morning Musume
bryan
46
6.2k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
Transcript
3Y4XJGUͷPCTFSWF0Oͱ TVCTDSJCF0OΛཧղ͢Δ େఉ ৻Ұ גࣜձࣾϝϧΧϦ / גࣜձࣾικ 2016/4/14 RxSwiftษڧձ ˏ
Sansan 1
ࣗݾհ 2 େఉ ৻Ұ ooba / bricklife גࣜձࣾϝϧΧϦ 20134݄ೖࣾ ݱࡏגࣜձࣾικग़த
ʮϝϧΧϦʯiOS൛ͷ্ཱͪ͛ ʮϝϧΧϦ ΞοςʯiOS൛ͷ্ཱͪ͛
ϝϧΧϦΞος 3 ʮͳΜͰืूͰ͖Δ ɹҬίϛϡχςΟΞϓϦʯ 201510݄ ։ൃ։࢝ 20162݄ հ੍Ͱެ։ 20163݄ ਖ਼ࣜΦʔϓϯ
RxSwiftΛશ໘࠾༻
PCTFSWF0OͱTVCTDSJCF0O 4
PCTFSWF0Oʹؔ͢Δ᷿ͷઆ໌ 5 observeOnΑΓ͋ͱʹॻ͔Ε͍ͯΔΦϖ Ϩʔλͷ࣮ߦεϨουΛܾΊΔͷ Θ͔Γ͍͢ ࣮ࡍਖ਼͍͠
TVCTDSJCF0Oʹؔ͢Δ᷿ͷઆ໌ 6 ετϦʔϜશମͷ࣮ߦεϨουΛܾΊΔ ͷʁ subscribeOnΑΓલʹॻ͔Ε͍ͯΔΦϖ Ϩʔλͷ࣮ߦεϨουΛ͖ΊΔͷʁ Կճॻ͍ͯҰ൪্ͷsubscribeOn͚͕ͩ ༗ޮʹͳΔʁ
TVCTDSJCF0OͷϑΝʔετΠϯϓϨογϣϯ 7 ීஈʮsubscribeʯͱ͍͏୯ޠΛΈΔͷ ετϦʔϜͷߪಡ෦͚ͩ ͦͷͨΊɺ࠷ॳݟͨ࣌ʹ subscribeNextͳͲͷߪಡ෦ͷ࣮ߦε ϨουΛܾΊΔͷͩͱࢥͬͨ େ͖ͳؒҧ͍
ެࣜͷઆ໌ 8 http://reactivex.io/documentation/scheduler.html
؆୯ͳ༻ྫ 9 ίʔυɿ Observable.just(1) .subscribeOn(backgroundScheduler) .map { $0 * 2
} .observeOn(MainScheduler.instance) .subscribe { print($0) } ग़ྗɿ Next(2) Completed
؆୯ͳ༻ྫ 10 ίʔυɿ Observable.just(1) .subscribeOn(backgroundScheduler) .map { $0 * 2
} .observeOn(MainScheduler.instance) .subscribe { print($0) } ग़ྗɿ Next(2) Completed όοΫάϥϯυεϨουͰ࣮ߦ
؆୯ͳ༻ྫ 11 ίʔυɿ Observable.just(1) .subscribeOn(backgroundScheduler) .map { $0 * 2
} .observeOn(MainScheduler.instance) .subscribe { print($0) } ग़ྗɿ Next(2) Completed ϝΠϯεϨουͰ࣮ߦ
0CTFSWFSͱ 12
0CTFSWFSͱ 13 on()ͱ͍͏ϝιου͕࣮͞Ε͍ͯΔ Observable͕ͦΕΛͬͯΠϕϯτΛୡ ͢Δ ͢ͳΘͪObserverType
0CTFSWFS5ZQF 14 protocol ObserverType { associatedtype E func on(event: Event<E>)
}
0CTFSWBCMFͱ 15
0CTFSWBCMFͱ 16 subscribe()ͱ͍͏ϝιου͕࣮͞Ε͍ͯ Δ Observer͕ͦΕΛͬͯߪಡͰ͖Δ ͢ͳΘͪObservableType
0CTFSWBCMF5ZQF 17 protocol ObservableType : ObservableConvertibleType { associatedtype E func
subscribe<O: ObserverType where O.E == E> (observer: O) -> Disposable }
ΦϖϨʔλͱ 18
ΦϖϨʔλͱ 19 ObservableΛsubscribeͯ͠ɺ৽͍͠ ObservableΛ࡞Δͷ ObserverͱObservableͷ྆ํͷಇ͖Λ͢ Δ RxSwiftͰશͯͷΦϖϨʔλʹΫϥε͕ ఆٛ͞Ε͍ͯΔ
ͨͱ͑NBQ 20 map() = Map + MapSink https://github.com/ReactiveX/RxSwift/blob/2.4/ RxSwift/Observable.swift https://github.com/ReactiveX/RxSwift/blob/2.4/
RxSwift/Observables/Implementations/Map.swift MapObservable MapSinkObserver
PCTFSWF0OTVCTDSJCF0Oʁ ͪΖΜobserveOnsubscribeOnʹΫ ϥε͕ఆٛ͞Ε͍ͯΔ https://github.com/ReactiveX/RxSwift/blob/2.4/ RxSwift/Observables/Implementations/ ObserveOn.swift https://github.com/ReactiveX/RxSwift/blob/2.4/ RxSwift/Observables/Implementations/ SubscribeOn.swift 21
ΦϖϨʔλ͕͍ͬͯΔ͜ͱ
PCTFSWF0OTVCTDSJCF0OΛΘͳ͍ྫ 23 Observable.just(1) .map { $0 * 2 } .subscribe
{ print($0) } +VTU .BQ4JOL .BQ "OPOZNPVT0CTFSWFS
ॲཧͷྲྀΕ 24 +VTU .BQ4JOL .BQ "OPOZNPVT0CTFSWFS
ॲཧͷྲྀΕ 25 1. AnonymousObserver͕Mapͷsubscribe()Λ࣮ߦ subscribe() ↑ +VTU .BQ4JOL .BQ "OPOZNPVT0CTFSWFS
ॲཧͷྲྀΕ 26 1. AnonymousObserver͕Mapͷsubscribe()Λ࣮ߦ 2. MapSink͕Justͷsubscribe()Λ࣮ߦ subscribe() ↑ +VTU .BQ4JOL
.BQ "OPOZNPVT0CTFSWFS subscribe() ↑
ॲཧͷྲྀΕ 27 1. AnonymousObserver͕Mapͷsubscribe()Λ࣮ߦ 2. MapSink͕Justͷsubscribe()Λ࣮ߦ 3. Just͕MapSinkͷon()Λ࣮ߦ subscribe() ↑
subscribe() ↑ ↓ on() +VTU .BQ4JOL .BQ "OPOZNPVT0CTFSWFS
ॲཧͷྲྀΕ 28 1. AnonymousObserver͕Mapͷsubscribe()Λ࣮ߦ 2. MapSink͕Justͷsubscribe()Λ࣮ߦ 3. Just͕MapSinkͷon()Λ࣮ߦ 4. Map͕AnonymousObserverͷon()Λ࣮ߦ
※ݫີʹͪΐͬͱҧ͏ subscribe() ↑ subscribe() ↑ ↓ on() ↓ on() +VTU .BQ4JOL .BQ "OPOZNPVT0CTFSWFS
PCTFSWF0OTVCTDSJCF0OΛͬͨྫ 29 Observable.just(1) .subscribeOn(backgroundScheduler) .map { $0 * 2 }
.observeOn(MainScheduler.instance) .subscribe { print($0) }
ॲཧͷྲྀΕ 30 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ 4VCTDSJCF0O4JOL 4VCTDSJCF0O +VTU
1. AnonymousObserver͕ObserveOnͷsubscribe()Λ࣮ ߦ ॲཧͷྲྀΕ 31 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ
4VCTDSJCF0O4JOL 4VCTDSJCF0O +VTU subscribe() ↑
2. ObserveOnSink͕Mapͷsubscribe()Λ࣮ߦ ॲཧͷྲྀΕ 32 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ 4VCTDSJCF0O4JOL
4VCTDSJCF0O +VTU subscribe() ↑ subscribe() ↑
3. MapSink͕SubscribeOnͷsubscribe()Λ࣮ߦ ॲཧͷྲྀΕ 33 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ 4VCTDSJCF0O4JOL
4VCTDSJCF0O +VTU subscribe() ↑ subscribe() ↑ subscribe() ↑
4. SubscribeOnSink͕Justͷsubscribe()Λ backgroundScheduler্Ͱ࣮ߦ ॲཧͷྲྀΕ 34 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ
4VCTDSJCF0O4JOL 4VCTDSJCF0O +VTU subscribe() ↑ subscribe() ↑ subscribe() ↑ subscribe() ↑
5. Just͕SubscribeOnSinkͷon()ΛbackgroundScheduler ্Ͱ࣮ߦ ॲཧͷྲྀΕ 35 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ
4VCTDSJCF0O4JOL 4VCTDSJCF0O +VTU subscribe() ↑ subscribe() ↑ subscribe() ↑ subscribe() ↑ ↓ on()
6. SubscribeOn͕MapSinkͷon()ΛbackgroundScheduler ্Ͱ࣮ߦ ॲཧͷྲྀΕ 36 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ
4VCTDSJCF0O4JOL 4VCTDSJCF0O +VTU subscribe() ↑ subscribe() ↑ subscribe() ↑ subscribe() ↑ ↓ on() ↓ on()
7. Map͕ObserveOnSinkͷon()ΛbackgroundScheduler ্Ͱ࣮ߦ ॲཧͷྲྀΕ 37 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ
4VCTDSJCF0O4JOL 4VCTDSJCF0O +VTU subscribe() ↑ subscribe() ↑ subscribe() ↑ subscribe() ↑ ↓ on() ↓ on() ↓ on()
8. ObserveOn͕AnonymousObserverͷon()Λ MainScheduler্Ͱ࣮ߦ ॲཧͷྲྀΕ 38 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ
4VCTDSJCF0O4JOL 4VCTDSJCF0O +VTU subscribe() ↑ subscribe() ↑ subscribe() ↑ subscribe() ↑ ↓ on() ↓ on() ↓ on() ↓ on()
ཁ͢Δʹ 39 • subscribeOnҎ߱(Observableํ)ͷ subscribe()ͷ࣮ߦεϨουΛܾΊΔ • observeOnҎ߱(Observerํ)ͷon() ͷ࣮ߦεϨουΛܾΊΔ ͱ͍͏ͷ͕ਖ਼͍͠ཧղʢͷͣʣ
TVCTDSJCF0O͕ޮ͔ͳ͍έʔε
TVCTDSJCF0O͕ޮ͔ͳ͍έʔεͦͷ̍ 41 NSURLSessionͷrx_response • ࣮ߦεϨουNSURLSessionͷઃఆͰܾ·Δ • ϦΫΤετΛ͛Δͱ͜ΖͰޮ͘
/463-4FTTJPOͷSY@SFTQPOTF 42 func rx_response(request: NSURLRequest) -> Observable<(NSData, NSHTTPURLResponse)> { return
Observable.create { observer in let task = self.dataTaskWithRequest(request) { (data, response, error) in guard let response = response, data = data else { observer.on(.Error(error ?? RxCocoaURLError.Unknown)) return } guard let httpResponse = response as? NSHTTPURLResponse else { observer.on(.Error(RxCocoaURLError.NonHTTPResponse(response: response))) return } observer.on(.Next(data, httpResponse)) observer.on(.Completed) } let t = task t.resume() return AnonymousDisposable { task.cancel() } } } https://github.com/ReactiveX/RxSwift/blob/2.4/ RxCocoa/Common/Observables/NSURLSession+Rx.swift この部分の実⾏スレッドはRxの機能では指定できない
TVCTDSJCF0O͕ޮ͔ͳ͍έʔεͦͷ̎ 43 ͢Ͱʹ࣮ߦεϨου͕ܾ·͍ͬͯΔͷ • Hot Observableશൠ - PublishSubjectBehaviorSubjectͳͲͷSubjectܥ - shareReplay(1)ͳͲͷΦϖϨʔλʹΑͬͯHotԽ͞Εͨ
Observable • UIKitܥͷRx֦ு • Driver
ݟ 44 ڍಈͷཧղʹࣗ৴͕ͳ͔ͬͨΒιʔεΛ ಡΉͱ͍͍ʢ͋ͨΓ·͑ʣ
͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ