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
CSC509 Lecture 11
javiergs
PRO
0
180
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
cmp.Or に感動した
otakakot
3
200
subpath importsで始めるモック生活
10tera
0
310
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
110
EMになってからチームの成果を最大化するために取り組んだこと/ Maximize team performance as EM
nashiusagi
0
100
Jakarta EE meets AI
ivargrimstad
0
210
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.8k
Amazon Qを使ってIaCを触ろう!
maruto
0
410
イベント駆動で成長して委員会
happymana
1
330
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
52
13k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Unsuck your backbone
ammeep
668
57k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
A Tale of Four Properties
chriscoyier
156
23k
Bash Introduction
62gerente
608
210k
10 Git Anti Patterns You Should be Aware of
lemiorhan
655
59k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.3k
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 ڍಈͷཧղʹࣗ৴͕ͳ͔ͬͨΒιʔεΛ ಡΉͱ͍͍ʢ͋ͨΓ·͑ʣ
͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ