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

RxSwift Observable.create

USAMI Kosuke
October 26, 2016

RxSwift Observable.create

USAMI Kosuke

October 26, 2016
Tweet

More Decks by USAMI Kosuke

Other Decks in Programming

Transcript

  1. ୯७ʹΠϕϯτΛྲྀ͢ Observable let variable = Variable("1") variable.value = "2" variable.value

    = "3" let subject = PublishSubject<String>() subject.onNext("A") subject.onNext("B")
  2. ϙΠϯτ (1) ΤϥʔΛૹΖ͏ let hoge = Observable.create { [unowned self]

    observer in do { let result = try self.hogehoge() observer.on(.next(result)) observer.on(.completed) } catch let error { observer.on(.error(error)) } return Disposables.create() }
  3. ϙΠϯτ (2) ΩϟϯηϧՄೳʹ͠Α͏ let hoge = Observable.create { [unowned self]

    observer in let task = self.dataTaskWithRequest(request) { ... } task.resume() return Disposables.create { task.cancel() // dispose ࣌ʹ͕࣮͜͜ߦ͞ΕΔ } }
  4. Ԡ༻ : ΩϟϯηϧϙΠϯτΛ࡞Δ let hoge = Observable.create { [unowned self]

    observer in self.scheduler.schedule(()) { _ in hogeTask1() // ͜͜ͰΩϟϯηϧͰ͖ΔΑ͏ʹ͍ͨ͠ hogeTask2() } return Disposables.create { ??? } }
  5. Ԡ༻ : ΩϟϯηϧϙΠϯτΛ࡞Δ let hoge = Observable.create { [unowned self]

    observer in let cancelDisposable = BooleanDisposable() let disposable = self.scheduler.schedule(()) { _ in hogeTask1() if cancelDisposable.disposed == true { return } hogeTask2() } return CompositeDisposable(disposable, cancelDisposable) }