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/

Shinichiro Oba

April 14, 2016
Tweet

More Decks by Shinichiro Oba

Other Decks in Programming

Transcript

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

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

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

    } .observeOn(MainScheduler.instance) .subscribe { print($0) } ग़ྗɿ Next(2) Completed ϝΠϯεϨουͰ࣮ߦ
  4. 0CTFSWBCMF5ZQF 17 protocol ObservableType : ObservableConvertibleType { associatedtype E func

    subscribe<O: ObserverType where O.E == E> (observer: O) -> Disposable }
  5. ॲཧͷྲྀΕ 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
  6. 4. SubscribeOnSink͕Justͷsubscribe()Λ backgroundScheduler্Ͱ࣮ߦ ॲཧͷྲྀΕ 34 0CTFSWF0O4JOL 0CTFSWF0O "OPOZNPVT0CTFSWFS .BQ4JOL .BQ

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

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

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

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

    4VCTDSJCF0O4JOL 4VCTDSJCF0O +VTU subscribe() ↑ subscribe() ↑ subscribe() ↑ subscribe() ↑ ↓ on() ↓ on() ↓ on() ↓ on()
  11. /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の機能では指定できない