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.3k
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.9k
ReactiveCocoaのゆるい紹介とメルカリでの活用事例
bricklife
3
3.6k
Apple Pay対応のやりかた
bricklife
1
25k
iOS版グローバル対応の罠と技
bricklife
8
24k
Other Decks in Programming
See All in Programming
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
140
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
130
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
550
testcontainers のススメ
sgash708
1
120
Recoilを剥がしている話
kirik
5
6.7k
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
5
1.2k
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
6
890
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
780
Security_for_introducing_eBPF
kentatada
0
110
わたしの星のままで一番星になる ~ 出産を機にSIerからEC事業会社に転職した話 ~
kimura_m_29
0
180
暇に任せてProxmoxコンソール 作ってみました
karugamo
2
720
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
45
7k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Become a Pro
speakerdeck
PRO
26
5k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
The Invisible Side of Design
smashingmag
298
50k
Facilitating Awesome Meetings
lara
50
6.1k
Navigating Team Friction
lara
183
15k
Raft: Consensus for Rubyists
vanstee
137
6.7k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
What's in a price? How to price your products and services
michaelherold
243
12k
Fireside Chat
paigeccino
34
3.1k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
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 ڍಈͷཧղʹࣗ৴͕ͳ͔ͬͨΒιʔεΛ ಡΉͱ͍͍ʢ͋ͨΓ·͑ʣ
͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ