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

ca.swift_2.pdf

4d40d82cc3c676e8a67ffc2a473bf423?s=47 Kyohei Ito
March 01, 2017
990

 ca.swift_2.pdf

4d40d82cc3c676e8a67ffc2a473bf423?s=128

Kyohei Ito

March 01, 2017
Tweet

Transcript

  1. RxSwift Operators Tips 2017/03/1 CA.swift

  2. About Me ҏ౻ɹګฏ Github : KyoheiG3 Twitter : @KyoheiG3

  3. None
  4. None
  5. ͖Ε͍ͳ઒ͷྲྀΕΛकΔͨΊͷ੍໿ͦͷᶃ ઒Λ෼அ͠ͳ͍

  6. ͖Ε͍ͳ઒ͷྲྀΕΛकΔͨΊͷ੍໿ͦͷᶃ → DisposeBagΛෳ਺ʹ෼͚ͳ͍

  7. flatMap

  8. flatMap Observable͔Βૹ৴͞ΕͨΞΠςϜΛObservablesʹม׵͠ɺ ͦͷޙͦΕΒ͔Βૹ৴͞ΕΔΞΠςϜΛ̍ͭͷObservable΁ม ׵͢Δɻ

  9. flatMap ઒͔Β઒΁ɺྲྀΕΛͭͳ͙

  10. Bad Stream botton.rx.tap .subscribe(onNext: { URLSession.shared.rx.json(request: request) .subscribe(onNext: { ...

    }) .addDisposableTo(disposeBag) }) .addDisposableTo(disposeBag)
  11. Good Stream botton.rx.tap .flatMap { URLSession.shared.rx.json(request: request) } .subscribe(onNext: {

    ... }) .addDisposableTo(disposeBag)
  12. flatMapͷಛ௃ લճͷΠϕϯτ͕ऴ͍ྃͯ͠ͳͯ͘΋ΞΠςϜ͕ετϦʔϜʹྲྀ ΕΔͷͰɺಉ͡ॲཧ͕ෳ਺ճߦΘΕΔՄೳੑ͕͋Δɻ

  13. ඇಉظॲཧͰ΍Γ͍ͨ͜ͱ ௨৴ͳͲͷඇಉظॲཧ͸ɺॲཧத͸ετϦʔϜΛྲྀ͞ͳ͍ɺ· ͨ͸࠷৽ͷετϦʔϜ͚ͩΛྲྀ͍ͨ͠ɻ

  14. flatMapLatest

  15. flatMapLatest ઒ͷྲྀΕΛஅͪ੾ͬͯɺ࣍ͷ઒΁ͱྲྀΕΛͭͳ͙

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

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

    ... }) .addDisposableTo(disposeBag)
  18. flatMapLatestͷಛ௃ ৽͍͠Πϕϯτ͕ىͬͨ͜ΒલͷΠϕϯτΛഁغͯ࣍͠ͷΠϕϯ τΛߦ͏ͷͰɺ୯Ұͷॲཧ͔͠ߦΘΕͳ͍ɻ Timer΍IntervalͳͲͷඇಉظॲཧ΋ɺflatMapLatestͱ૬ੑ͕ Α͍ɻ

  19. Timer Stream botton.rx.tap .flatMapLatest { Observable<Int>.timer(1, scheduler: scheduler) } .subscribe(onNext:

    { ... }) .addDisposableTo(disposeBag)
  20. debounceͱॲཧ͕Α͘ࣅ͍ͯΔ

  21. Debounce botton.rx.tap .debounce(1, scheduler: scheduler) .subscribe(onNext: { ... }) .addDisposableTo(disposeBag)

  22. DebounceͱTimer Streamͷҧ͍ ετϦʔϜʹྲྀΕ͖ͯͨ஋Λ΋ͱʹintervalΛม͑Δ͜ͱ͕Ͱ ͖Δɻ

  23. έʔεͦͷᶃ ΞΠςϜΛड͚औ͔ͬͯΒɺҰఆ࣌ؒޙʹΞΠςϜΛετϦʔϜ ʹྲྀ͍ͨ͠ɻ஗Ԇ࣌ؒ͸ΞΠςϜͷ಺༰ʹΑͬͯ൑அ͢Δɻ

  24. { "num": 10, "title": "Timer Stream" }

  25. 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)
  26. έʔεͦͷᶄ ΞΠςϜΛड͚औͬͨΒετϦʔϜʹྲྀ͕͢ɺҰఆ࣌ؒޙʹnil ΛετϦʔϜʹྲྀ͍ͨ͠ɻ஗Ԇ࣌ؒ͸ΞΠςϜͷ಺༰ʹΑͬͯ ൑அ͢Δɻ

  27. έʔεͦͷᶄ startWithΛ࢖͏

  28. 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)
  29. flatMapFirst

  30. flatMapFirst ઒ͷྲྀΕ͕ࢭ·Δ·Ͱɺ࣍ͷ઒΁͸ͭͳ͕ͳ͍

  31. 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)
  32. Good Stream botton.rx.tap .flatMapFirst { URLSession.shared.rx.json(request: request) } .subscribe(onNext: {

    ... }) .addDisposableTo(disposeBag)
  33. flatMapFirstͷಛ௃ ࠷ॳʹૹ৴͞ΕͨΞΠςϜͷॲཧ͕׬ྃ͢Δ·Ͱ͸ɺͦͷޙʹ ૹΒΕͨΞΠςϜΛ఻ൖ͠ͳ͍ͷͰɺ୯Ұͷॲཧ͔͠ߦΘΕͳ ͍ɻ

  34. Timer Stream botton.rx.tap .flatMapFirst { Observable<Int>.timer(1, scheduler: scheduler) } .subscribe(onNext:

    { ... }) .addDisposableTo(disposeBag)
  35. throttleͱॲཧ͕Α͘ࣅ͍ͯΔ

  36. Throttle botton.rx.tap .throttle(1, scheduler: scheduler) .subscribe(onNext: { ... }) .addDisposableTo(disposeBag)

  37. Throttleͷ஫ҙ఺ throttle͸ετϦʔϜ1ൃ໨ͷΞΠςϜ͕ྲྀΕͯ͘ΔͬΆ͍ɻ ੲ͸ྲྀΕͯ͜ͳ͔ͬͨͷͰɺ΋͔ͨ͠͠ΒόʔδϣϯʹΑͬͯ มΘΔ͔΋ɻ

  38. None
  39. ͖Ε͍ͳ઒ͷྲྀΕΛकΔͨΊͷ੍໿ͦͷᶄ ઒ʹ΋ͷΛ౤͛ೖΕͳ͍

  40. ͖Ε͍ͳ઒ͷྲྀΕΛकΔͨΊͷ੍໿ͦͷᶄ → ΫϩʔδϟͰselfΛࢀরͤ͞ͳ͍

  41. selfΛࢀরͤ͞ͳ͍ํ๏ ෳ਺ͷετϦʔϜΛ߹੒ɺ͋Δ͍͸ঢ়ଶΛ஌Δඞཁ͕͋Δɻ RxͷoperatorsҰཡͳͲΛ֬ೝͯ͠Έͯɺ(x, y)Έ͍ͨʹ2ͭͷ ஋Λࢀর͍ͯ͠ΔΑ͏ͳoperator͸߹੒νϟϯεɻ ׬શʹࢀর͠ͳ͍ͷ͸೉͍͠ͷͰɺͰ͖ΔݶΓͰ΍ͬͯΈΔɻ

  42. withLatestFrom

  43. withLatestFrom ͋ΔετϦʔϜʹରͯ͠ɺผͷετϦʔϜͷ࠷৽ͷΞΠςϜΛ߹ ੒͢Δɻ

  44. Bad Stream let other = Variable<Int>(0) botton.rx.tap .subscribe(onNext: { [weak

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

    self] in print(self?.other.value) }) .addDisposableTo(disposeBag)
  46. Good Stream let other = Variable<Int>(0) botton.rx.tap .withLatestFrom(other.asObservable()) .subscribe(onNext: {

    ... }) .addDisposableTo(disposeBag)
  47. withLatestFromͷಛ௃ subscribeޙʹετϦʔϜʹྲྀΕͨ࠷৽ͷΞΠςϜͰ͋Ε͹ɺ VariableͷΑ͏ʹ஋Λอ͍࣋ͯ͠ͳͯ͘΋͍֮͑ͯͯ͘ΕΔͷ Ͱ߹੒͢Δ͜ͱ͕Ͱ͖Δɻ

  48. Good Stream let other = PublishSubject<Int>() botton.rx.tap .withLatestFrom(other) .subscribe(onNext: {

    ... }) .addDisposableTo(disposeBag)
  49. scan

  50. scan Observable͔Βૹ৴͞ΕΔΞΠςϜʹ࿈ଓͯؔ͠਺Λద༻͢ Δɻ

  51. 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)
  52. 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)
  53. scanͷಛ௃ scanΛߦ͏͜ͱͰɺ͋ΔετϦʔϜʹରͯ͠ผͷΞΠςϜΛྲྀ ͢͜ͱ͕Ͱ͖Δɻ ετϦʔϜʹྲྀͨ͠ΞΠςϜ͸อ࣋͞Ε͍ͯΔͷͰɺ࣍ճετ ϦʔϜʹΞΠςϜ͕ྲྀΕ͖ͯͨࡍʹɺอ͍࣋ͯ͠ΔΞΠςϜͱ ྲྀΕ͖ͯͨΞΠςϜͷ྆ํΛಘΔ͜ͱ͕Մೳɻ

  54. scanͷಛ௃ ৚݅ʹ߹Θͳ͚Ε͹ಉҰͷΞΠςϜΛྲྀ͠ଓ͚ͯɺ distinctUntilChangedΛ࣮ߦͯ͠ॲཧΛল͘͜ͱ΋Ͱ͖Δɻ

  55. 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)
  56. scanͷಛ௃ อ͍࣋ͯ͠ΔΞΠςϜ͕มΘΔࡍʹɺݹ͍ΞΠςϜΛར༻ͯ͠ॲ ཧΛߦ͏Α͏ͳ͜ͱ΋Մೳɻ

  57. 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)
  58. distinctUntilChanged

  59. distinctUntilChanged ॏෳͨ͠ΞΠςϜͷ৔߹͸ૹ৴͠ͳ͍Α͏ʹ͢ΔΦϖϨʔλɻ

  60. distinctUntilChanged botton.rx.tap .flatMapLatest { URLSession.shared.rx.json(request: request) } .map { json

    in json["num"] as! Int } .distinctUntilChanged() .subscribe(onNext: { ... }) .addDisposableTo(disposeBag)
  61. distinctUntilChangedͷಛ௃ ΞΠςϜ͕EquatableͰ͸ͳ͍৔߹͸ΫϩʔδϟΛड͚औͬͯͦ ͷΫϩʔδϟΛ࣮ߦ͠ɺ໭Γ஋͕trueͷ͏ͪ͸ॲཧΛܧଓ͠ͳ ͍Α͏ͳৼΔ෣͍Λ͍ͯ͠Δɻ

  62. distinctUntilChangedͷಛ௃ ετϦʔϜʹྲྀΕͨΞΠςϜΛอ͓͖࣋ͯ͠ɺ࣍ճετϦʔϜʹ ΞΠςϜ͕ྲྀΕ͖ͯͨࡍʹ৽چ྆ํͷΞΠςϜΛಘΔ͜ͱ͕Մ ೳɻ ॳظ஋Λؾʹ͠ͳ͍ͷͰ͋Ε͹ɺscanͱಉ͡Α͏ͳॲཧΛ΋͏ গ͠୹͘ॻ͘͜ͱ΋Ͱ͖Δɻ

  63. 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)
  64. ·ͱΊ

  65. None
  66. Thanks!