Slide 1

Slide 1 text

3Y4XJGUͷPCTFSWF0Oͱ TVCTDSJCF0OΛཧղ͢Δ େఉ ৻Ұ࿠ גࣜձࣾϝϧΧϦ / גࣜձࣾι΢κ΢ 2016/4/14 RxSwiftษڧձ ˏ Sansan 1

Slide 2

Slide 2 text

ࣗݾ঺հ 2 େఉ ৻Ұ࿠ ooba / bricklife גࣜձࣾϝϧΧϦ 2013೥4݄ೖࣾ ݱࡏ͸גࣜձࣾι΢κ΢΁ग़޲த ʮϝϧΧϦʯiOS൛ͷ্ཱͪ͛ ʮϝϧΧϦ ΞοςʯiOS൛ͷ্ཱͪ͛

Slide 3

Slide 3 text

ϝϧΧϦΞος 3 ʮͳΜͰ΋ืूͰ͖Δ ɹ஍ҬίϛϡχςΟΞϓϦʯ 2015೥10݄ ։ൃ։࢝ 2016೥2݄ ঺հ੍Ͱެ։ 2016೥3݄ ਖ਼ࣜΦʔϓϯ RxSwiftΛશ໘࠾༻

Slide 4

Slide 4 text

PCTFSWF0OͱTVCTDSJCF0O 4

Slide 5

Slide 5 text

PCTFSWF0Oʹؔ͢Δ᷿ͷઆ໌ 5 observeOnΑΓ͋ͱʹॻ͔Ε͍ͯΔΦϖ Ϩʔλͷ࣮ߦεϨουΛܾΊΔ΋ͷ Θ͔Γ΍͍͢ ࣮ࡍਖ਼͍͠

Slide 6

Slide 6 text

TVCTDSJCF0Oʹؔ͢Δ᷿ͷઆ໌ 6 ετϦʔϜશମͷ࣮ߦεϨουΛܾΊΔ΋ ͷʁ subscribeOnΑΓલʹॻ͔Ε͍ͯΔΦϖ Ϩʔλͷ࣮ߦεϨουΛ͖ΊΔ΋ͷʁ Կճॻ͍ͯ΋Ұ൪্ͷsubscribeOn͚͕ͩ ༗ޮʹͳΔʁ

Slide 7

Slide 7 text

TVCTDSJCF0OͷϑΝʔετΠϯϓϨογϣϯ 7 ීஈʮsubscribeʯͱ͍͏୯ޠΛΈΔͷ͸ ετϦʔϜͷߪಡ෦෼͚ͩ ͦͷͨΊɺ࠷ॳݟͨ࣌ʹ๻͸ subscribeNextͳͲͷߪಡ෦෼ͷ࣮ߦε ϨουΛܾΊΔ΋ͷͩͱࢥͬͨ େ͖ͳؒҧ͍

Slide 8

Slide 8 text

ެࣜͷઆ໌ 8 http://reactivex.io/documentation/scheduler.html

Slide 9

Slide 9 text

؆୯ͳ࢖༻ྫ 9 ίʔυɿ Observable.just(1) .subscribeOn(backgroundScheduler) .map { $0 * 2 } .observeOn(MainScheduler.instance) .subscribe { print($0) } ग़ྗɿ Next(2) Completed

Slide 10

Slide 10 text

؆୯ͳ࢖༻ྫ 10 ίʔυɿ Observable.just(1) .subscribeOn(backgroundScheduler) .map { $0 * 2 } .observeOn(MainScheduler.instance) .subscribe { print($0) } ग़ྗɿ Next(2) Completed όοΫάϥ΢ϯυεϨουͰ࣮ߦ

Slide 11

Slide 11 text

؆୯ͳ࢖༻ྫ 11 ίʔυɿ Observable.just(1) .subscribeOn(backgroundScheduler) .map { $0 * 2 } .observeOn(MainScheduler.instance) .subscribe { print($0) } ग़ྗɿ Next(2) Completed ϝΠϯεϨουͰ࣮ߦ

Slide 12

Slide 12 text

0CTFSWFSͱ͸ 12

Slide 13

Slide 13 text

0CTFSWFSͱ͸ 13 on()ͱ͍͏ϝιου͕࣮૷͞Ε͍ͯΔ Observable͕ͦΕΛ࢖ͬͯΠϕϯτΛ఻ୡ ͢Δ ͢ͳΘͪObserverType

Slide 14

Slide 14 text

0CTFSWFS5ZQF 14 protocol ObserverType { associatedtype E func on(event: Event) }

Slide 15

Slide 15 text

0CTFSWBCMFͱ͸ 15

Slide 16

Slide 16 text

0CTFSWBCMFͱ͸ 16 subscribe()ͱ͍͏ϝιου͕࣮૷͞Ε͍ͯ Δ Observer͕ͦΕΛ࢖ͬͯߪಡͰ͖Δ ͢ͳΘͪObservableType

Slide 17

Slide 17 text

0CTFSWBCMF5ZQF 17 protocol ObservableType : ObservableConvertibleType { associatedtype E func subscribe (observer: O) -> Disposable }

Slide 18

Slide 18 text

ΦϖϨʔλͱ͸ 18

Slide 19

Slide 19 text

ΦϖϨʔλͱ͸ 19 ObservableΛsubscribeͯ͠ɺ৽͍͠ ObservableΛ࡞Δ΋ͷ ObserverͱObservableͷ྆ํͷಇ͖Λ͢ Δ RxSwiftͰ͸શͯͷΦϖϨʔλʹΫϥε͕ ఆٛ͞Ε͍ͯΔ

Slide 20

Slide 20 text

ͨͱ͑͹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 Map͸Observable MapSink͸Observer

Slide 21

Slide 21 text

PCTFSWF0O΍TVCTDSJCF0O͸ʁ ΋ͪΖΜobserveOn΍subscribeOnʹ΋Ϋ ϥε͕ఆٛ͞Ε͍ͯΔ 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

Slide 22

Slide 22 text

ΦϖϨʔλ͕΍͍ͬͯΔ͜ͱ

Slide 23

Slide 23 text

PCTFSWF0O΍TVCTDSJCF0OΛ࢖Θͳ͍ྫ 23 Observable.just(1) .map { $0 * 2 } .subscribe { print($0) } +VTU .BQ4JOL .BQ "OPOZNPVT0CTFSWFS

Slide 24

Slide 24 text

ॲཧͷྲྀΕ 24 +VTU .BQ4JOL .BQ "OPOZNPVT0CTFSWFS

Slide 25

Slide 25 text

ॲཧͷྲྀΕ 25 1. AnonymousObserver͕Mapͷsubscribe()Λ࣮ߦ subscribe() ↑ +VTU .BQ4JOL .BQ "OPOZNPVT0CTFSWFS

Slide 26

Slide 26 text

ॲཧͷྲྀΕ 26 1. AnonymousObserver͕Mapͷsubscribe()Λ࣮ߦ 2. MapSink͕Justͷsubscribe()Λ࣮ߦ subscribe() ↑ +VTU .BQ4JOL .BQ "OPOZNPVT0CTFSWFS subscribe() ↑

Slide 27

Slide 27 text

ॲཧͷྲྀΕ 27 1. AnonymousObserver͕Mapͷsubscribe()Λ࣮ߦ 2. MapSink͕Justͷsubscribe()Λ࣮ߦ 3. Just͕MapSinkͷon()Λ࣮ߦ subscribe() ↑ subscribe() ↑ ↓ on() +VTU .BQ4JOL .BQ "OPOZNPVT0CTFSWFS

Slide 28

Slide 28 text

ॲཧͷྲྀΕ 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

Slide 29

Slide 29 text

PCTFSWF0O΍TVCTDSJCF0OΛ࢖ͬͨྫ 29 Observable.just(1) .subscribeOn(backgroundScheduler) .map { $0 * 2 } .observeOn(MainScheduler.instance) .subscribe { print($0) }

Slide 30

Slide 30 text

ॲཧͷྲྀΕ 30 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ 4VCTDSJCF0O4JOL 4VCTDSJCF0O +VTU

Slide 31

Slide 31 text

1. AnonymousObserver͕ObserveOnͷsubscribe()Λ࣮ ߦ ॲཧͷྲྀΕ 31 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ 4VCTDSJCF0O4JOL 4VCTDSJCF0O +VTU subscribe() ↑

Slide 32

Slide 32 text

2. ObserveOnSink͕Mapͷsubscribe()Λ࣮ߦ ॲཧͷྲྀΕ 32 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ 4VCTDSJCF0O4JOL 4VCTDSJCF0O +VTU subscribe() ↑ subscribe() ↑

Slide 33

Slide 33 text

3. MapSink͕SubscribeOnͷsubscribe()Λ࣮ߦ ॲཧͷྲྀΕ 33 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ 4VCTDSJCF0O4JOL 4VCTDSJCF0O +VTU subscribe() ↑ subscribe() ↑ subscribe() ↑

Slide 34

Slide 34 text

4. SubscribeOnSink͕Justͷsubscribe()Λ backgroundScheduler্Ͱ࣮ߦ ॲཧͷྲྀΕ 34 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ 4VCTDSJCF0O4JOL 4VCTDSJCF0O +VTU subscribe() ↑ subscribe() ↑ subscribe() ↑ subscribe() ↑

Slide 35

Slide 35 text

5. Just͕SubscribeOnSinkͷon()ΛbackgroundScheduler ্Ͱ࣮ߦ ॲཧͷྲྀΕ 35 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ 4VCTDSJCF0O4JOL 4VCTDSJCF0O +VTU subscribe() ↑ subscribe() ↑ subscribe() ↑ subscribe() ↑ ↓ on()

Slide 36

Slide 36 text

6. SubscribeOn͕MapSinkͷon()ΛbackgroundScheduler ্Ͱ࣮ߦ ॲཧͷྲྀΕ 36 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ 4VCTDSJCF0O4JOL 4VCTDSJCF0O +VTU subscribe() ↑ subscribe() ↑ subscribe() ↑ subscribe() ↑ ↓ on() ↓ on()

Slide 37

Slide 37 text

7. Map͕ObserveOnSinkͷon()ΛbackgroundScheduler ্Ͱ࣮ߦ ॲཧͷྲྀΕ 37 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ 4VCTDSJCF0O4JOL 4VCTDSJCF0O +VTU subscribe() ↑ subscribe() ↑ subscribe() ↑ subscribe() ↑ ↓ on() ↓ on() ↓ on()

Slide 38

Slide 38 text

8. ObserveOn͕AnonymousObserverͷon()Λ MainScheduler্Ͱ࣮ߦ ॲཧͷྲྀΕ 38 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ 4VCTDSJCF0O4JOL 4VCTDSJCF0O +VTU subscribe() ↑ subscribe() ↑ subscribe() ↑ subscribe() ↑ ↓ on() ↓ on() ↓ on() ↓ on()

Slide 39

Slide 39 text

ཁ͢Δʹ 39 • subscribeOn͸Ҏ߱(Observableํ޲)ͷ subscribe()ͷ࣮ߦεϨουΛܾΊΔ • observeOn͸Ҏ߱(Observerํ޲)ͷon() ͷ࣮ߦεϨουΛܾΊΔ ͱ͍͏ͷ͕ਖ਼͍͠ཧղʢͷ͸ͣʣ

Slide 40

Slide 40 text

TVCTDSJCF0O͕ޮ͔ͳ͍έʔε

Slide 41

Slide 41 text

TVCTDSJCF0O͕ޮ͔ͳ͍έʔεͦͷ̍ 41 NSURLSessionͷrx_response • ࣮ߦεϨου͸NSURLSessionͷઃఆͰܾ·Δ • ϦΫΤετΛ౤͛Δͱ͜ΖͰ͸ޮ͘

Slide 42

Slide 42 text

/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の機能では指定できない

Slide 43

Slide 43 text

TVCTDSJCF0O͕ޮ͔ͳ͍έʔεͦͷ̎ 43 ͢Ͱʹ࣮ߦεϨου͕ܾ·͍ͬͯΔ΋ͷ • Hot Observableશൠ - PublishSubject΍BehaviorSubjectͳͲͷSubjectܥ - shareReplay(1)ͳͲͷΦϖϨʔλʹΑͬͯHotԽ͞Εͨ Observable • UIKitܥͷRx֦ு • Driver

Slide 44

Slide 44 text

஌ݟ 44 ڍಈͷཧղʹࣗ৴͕ͳ͔ͬͨΒιʔεΛ ಡΉͱ͍͍ʢ͋ͨΓ·͑ʣ

Slide 45

Slide 45 text

͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ