Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RxSwiftのobserveOnとsubscribeOnを理解する

 RxSwiftのobserveOnとsubscribeOnを理解する

RxSwift勉強会 @ Sansanの発表資料です
http://connpass.com/event/27933/

86aef9515e5986c096bc398c8990992e?s=128

Shinichiro Oba

April 14, 2016
Tweet

Transcript

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

    Sansan 1
  2. ࣗݾ঺հ 2 େఉ ৻Ұ࿠ ooba / bricklife גࣜձࣾϝϧΧϦ 2013೥4݄ೖࣾ ݱࡏ͸גࣜձࣾι΢κ΢΁ग़޲த

    ʮϝϧΧϦʯiOS൛ͷ্ཱͪ͛ ʮϝϧΧϦ ΞοςʯiOS൛ͷ্ཱͪ͛
  3. ϝϧΧϦΞος 3 ʮͳΜͰ΋ืूͰ͖Δ ɹ஍ҬίϛϡχςΟΞϓϦʯ 2015೥10݄ ։ൃ։࢝ 2016೥2݄ ঺հ੍Ͱެ։ 2016೥3݄ ਖ਼ࣜΦʔϓϯ

    RxSwiftΛશ໘࠾༻
  4. PCTFSWF0OͱTVCTDSJCF0O 4

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

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

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

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

  9. ؆୯ͳ࢖༻ྫ 9 ίʔυɿ Observable.just(1) .subscribeOn(backgroundScheduler) .map { $0 * 2

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

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

    } .observeOn(MainScheduler.instance) .subscribe { print($0) } ग़ྗɿ Next(2) Completed ϝΠϯεϨουͰ࣮ߦ
  12. 0CTFSWFSͱ͸ 12

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

  14. 0CTFSWFS5ZQF 14 protocol ObserverType { associatedtype E func on(event: Event<E>)

    }
  15. 0CTFSWBCMFͱ͸ 15

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

  17. 0CTFSWBCMF5ZQF 17 protocol ObservableType : ObservableConvertibleType { associatedtype E func

    subscribe<O: ObserverType where O.E == E> (observer: O) -> Disposable }
  18. ΦϖϨʔλͱ͸ 18

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

  20. ͨͱ͑͹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
  21. 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

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

  23. PCTFSWF0O΍TVCTDSJCF0OΛ࢖Θͳ͍ྫ 23 Observable.just(1) .map { $0 * 2 } .subscribe

    { print($0) } +VTU .BQ4JOL .BQ "OPOZNPVT0CTFSWFS
  24. ॲཧͷྲྀΕ 24 +VTU .BQ4JOL .BQ "OPOZNPVT0CTFSWFS

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

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

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

    subscribe() ↑ ↓ on() +VTU .BQ4JOL .BQ "OPOZNPVT0CTFSWFS
  28. ॲཧͷྲྀΕ 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
  29. PCTFSWF0O΍TVCTDSJCF0OΛ࢖ͬͨྫ 29 Observable.just(1) .subscribeOn(backgroundScheduler) .map { $0 * 2 }

    .observeOn(MainScheduler.instance) .subscribe { print($0) }
  30. ॲཧͷྲྀΕ 30 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ 4VCTDSJCF0O4JOL 4VCTDSJCF0O +VTU

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

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

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

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

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

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

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

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

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

  40. TVCTDSJCF0O͕ޮ͔ͳ͍έʔε

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

  42. /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の機能では指定できない
  43. TVCTDSJCF0O͕ޮ͔ͳ͍έʔεͦͷ̎ 43 ͢Ͱʹ࣮ߦεϨου͕ܾ·͍ͬͯΔ΋ͷ • Hot Observableશൠ - PublishSubject΍BehaviorSubjectͳͲͷSubjectܥ - shareReplay(1)ͳͲͷΦϖϨʔλʹΑͬͯHotԽ͞Εͨ

    Observable • UIKitܥͷRx֦ு • Driver
  44. ஌ݟ 44 ڍಈͷཧղʹࣗ৴͕ͳ͔ͬͨΒιʔεΛ ಡΉͱ͍͍ʢ͋ͨΓ·͑ʣ

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