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
testcontainers のススメ
sgash708
1
120
良いユニットテストを書こう
mototakatsu
5
1.9k
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
Go の GC の不得意な部分を克服したい
taiyow
2
770
CSC305 Lecture 26
javiergs
PRO
0
140
事業成長を爆速で進めてきたプロダクトエンジニアたちの成功談・失敗談
nealle
3
1.4k
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
140
17年周年のWebアプリケーションにTanStack Queryを導入する / Implementing TanStack Query in a 17th Anniversary Web Application
saitolume
0
250
これが俺の”自分戦略” プロセスを楽しんでいこう! - Developers CAREER Boost 2024
niftycorp
PRO
0
190
return文におけるstd::moveについて
onihusube
1
860
From Translations to Multi Dimension Entities
alexanderschranz
2
130
Security_for_introducing_eBPF
kentatada
0
110
Featured
See All Featured
How GitHub (no longer) Works
holman
311
140k
4 Signs Your Business is Dying
shpigford
181
21k
Side Projects
sachag
452
42k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Building Applications with DynamoDB
mza
91
6.1k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
28
900
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Code Reviewing Like a Champion
maltzj
520
39k
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 ڍಈͷཧղʹࣗ৴͕ͳ͔ͬͨΒιʔεΛ ಡΉͱ͍͍ʢ͋ͨΓ·͑ʣ
͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ