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

ca.swift_2.pdf

Kyohei Ito
March 01, 2017
1.2k

 ca.swift_2.pdf

Kyohei Ito

March 01, 2017
Tweet

Transcript

  1. Bad Stream botton.rx.tap .subscribe(onNext: { requestDisposeBag = DisposeBag() URLSession.shared.rx.json(request: request)

    .subscribe(onNext: { ... }) .addDisposableTo(requestDisposeBag) }) .addDisposableTo(disposeBag)
  2. Timer Stream botton.rx.tap .flatMapLatest { URLSession.shared.rx.json(request: request) } .flatMapLatest {

    json -> Observable<Any?> in Observable<Int>.timer(json["num"] as! Int, scheduler: scheduler) .map { _ in json } } .subscribe(onNext: { ... }) .addDisposableTo(disposeBag)
  3. Timer Stream botton.rx.tap .flatMapLatest { URLSession.shared.rx.json(request: request) } .flatMapLatest {

    json -> Observable<Any?> in Observable<Int>.timer(json["num"] as! Int, scheduler: scheduler) .map { _ in nil } .startWith(json) } .subscribe(onNext: { ... }) .addDisposableTo(disposeBag)
  4. Bad Stream var requesting = false botton.rx.tap .subscribe(onNext: { if

    requesting { return } requesting = true requestDisposeBag = DisposeBag() URLSession.shared.rx.json(request: request) .subscribe(onNext: { requesting = false }) .addDisposableTo(requestDisposeBag) }) .addDisposableTo(disposeBag)
  5. Bad Stream let other = Variable<Int>(0) botton.rx.tap .subscribe(onNext: { [weak

    self] in self?.other.asObservable() .subscribe(onNext: { ... }) .addDisposableTo(disposeBag) }) .addDisposableTo(disposeBag)
  6. Self Reference let other = Variable<Int>(0) botton.rx.tap .subscribe(onNext: { [weak

    self] in print(self?.other.value) }) .addDisposableTo(disposeBag)
  7. Bad Stream let some = Variable<Int>(0) botton.rx.tap .flatMapLatest { URLSession.shared.rx.json(request:

    request) } .map { [weak self] json in let num = json["num"] as! Int if let value = self?.some.value { let new = value + num self?.some.value = new return new } return 0 } .subscribe(onNext: { ... }) .addDisposableTo(disposeBag)
  8. Good Stream botton.rx.tap .flatMapLatest { URLSession.shared.rx.json(request: request) } .map {

    json in json["num"] as! Int } .scan(0) { some, new in some + new } .subscribe(onNext: { ... }) .addDisposableTo(disposeBag)
  9. Good Stream botton.rx.tap .flatMapLatest { URLSession.shared.rx.json(request: request) } .map {

    json in json["num"] as! Int } .scan(0) { some, new in new > some ? new : some } .distinctUntilChanged() .subscribe(onNext: { ... }) .addDisposableTo(disposeBag)
  10. Good Stream botton.rx.tap .flatMapLatest { URLSession.shared.rx.json(request: request) } .map {

    json in json["num"] as! Int } .scan(0) { some, new in if new > some { ... return new } return some } .distinctUntilChanged() .subscribe(onNext: { ... }) .addDisposableTo(disposeBag)
  11. distinctUntilChanged botton.rx.tap .flatMapLatest { URLSession.shared.rx.json(request: request) } .map { json

    in json["num"] as! Int } .distinctUntilChanged() .subscribe(onNext: { ... }) .addDisposableTo(disposeBag)
  12. distinctUntilChanged botton.rx.tap .flatMapLatest { URLSession.shared.rx.json(request: request) } .map { json

    in json["num"] as! Int } .distinctUntilChanged { old, new in if (new <= old) == false { ... } return new <= old } .subscribe(onNext: { ... }) .addDisposableTo(disposeBag)