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
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
440
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
9
1k
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
140
Nuxtベースの「WXT」でChrome拡張を作成する | Vue Fes 2024 ランチセッション
moshi1121
1
520
Server Driven Compose With Firebase
skydoves
0
400
のびしろを広げる巻き込まれ力:偶然を活かすキャリアの作り方/oso2024
takahashiikki
1
410
飲食業界向けマルチプロダクトを実現させる開発体制とリアルな現状
hiroya0601
1
390
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
23k
現場で役立つモデリング 超入門
masuda220
PRO
13
2.9k
Progressive Web Apps für Desktop und Mobile mit Angular (Hands-on)
christianliebel
PRO
0
110
Content Security Policy入門 セキュリティ設定と 違反レポートのはじめ方 / Introduction to Content Security Policy Getting Started with Security Configuration and Violation Reporting
uskey512
1
430
go.mod、DockerfileやCI設定に分散しがちなGoのバージョンをまとめて管理する / Go Connect #3
arthur1
10
2.4k
Featured
See All Featured
BBQ
matthewcrist
85
9.3k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Scaling GitHub
holman
458
140k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
790
Fontdeck: Realign not Redesign
paulrobertlloyd
81
5.2k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
167
49k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
355
29k
Statistics for Hackers
jakevdp
796
220k
Ruby is Unlike a Banana
tanoku
96
11k
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 ڍಈͷཧղʹࣗ৴͕ͳ͔ͬͨΒιʔεΛ ಡΉͱ͍͍ʢ͋ͨΓ·͑ʣ
͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ