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

RxSwiftを始めよう!

 RxSwiftを始めよう!

【朝活】iOS開発会議68回目 in 株式会社トライフォート
2016年6月30日
RxSwiftとRxの概念を解説しました。
https://www.facebook.com/events/920402871415186/

53e2d354b3299d64a54af680865516d5?s=128

Sato Takeshi

July 07, 2016
Tweet

More Decks by Sato Takeshi

Other Decks in Technology

Transcript

  1. ʲே׆ʳiOS։ൃձٞ68ճ໨ in גࣜձࣾτϥΠϑΥʔτ RxSwiftΛ࢝ΊΑ͏ʂ 2016೥6݄30೔ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 1

  2. ൃදऀʹ͍ͭͯ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 2

  3. ࠤ౻߶࢜(@hatakenokakashi) ༗ݶձࣾΧΠΧΠͰιϑτΤϯδχΞΛ͍ͯ͠·͢ɻiOS։ൃΛ த৺ʹɺɹαʔόʔ࡞ͬͨΓαΠτͭͬͨ͘ΓΘΓ͔͠ԿͰ΋ ͜ͳ͠·͢ɻ ։ൃܦݧɿ5೥͘Β͍ɺObjCɺSwiftɺPHPɺLinuxɺRubyɺ HTLMɺCSSɺJavaScript Swiftϓϩάϥϛϯάษڧձ 2016/6/30 3

  4. RxSwiftΛ࢝ΊΑ͏ʂ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 4

  5. ࠷ۙ ϦΞΫςΟϒϓϩάϥϛϯά͕ ਓؾͰ͢ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 5

  6. 4݄ʹཱͯଓ͚ʹΠϕϯτ͕ଓ͍ͨΓɻ http://connpass.com/search/?q=rxswift Swiftϓϩάϥϛϯάษڧձ 2016/6/30 6

  7. Qiitaͷهࣄ͕૿͍͑ͯͨΓɻ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 7

  8. ͦΕʹ৮ൃ͞ΕͯɺϦΞΫςΟϒϓϩ άϥϛϯάͷϑϨʔϜϫʔΫͷ̍ͭ RxSwiftΛ࢖ͬͯΈ·ͨ͠ɻ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 8

  9. ͜Ε͕ͱͬͯ΋ྑ͔ͬͨɻ ίʔυΛॻ͘ͷ͕ؾ͍͍࣋ͪʂ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 9

  10. ࠓ೔͸ࢲ͕RxSwiftΛೋϲ݄࢖ͬͯΘ ͔ͬͨ͜ͱɺ͍͍ͱ͜ΖΛڞ༗͍ͨ͠ ͱࢥ͍·͢ɻ ·ͩ·ͩ࢖͍͜ͳͤͯͳ͍ͷͰɺποίϛ͓ئ͍͠·͢ɻ ! Swiftϓϩάϥϛϯάษڧձ 2016/6/30 10

  11. RxSwiftͬͯͳʹʁ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 11

  12. Reactive Extensions(Rx) ϥΠϒϥϦʔͷҰछ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 12

  13. Reactive Extensions(Rx)ͬͯͳ ʹʁ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 13

  14. Reactive Extensions(Rx)ͬͯͳʹʁ • ඇಉظॲཧ΍ΠϕϯτॲཧͳͲΛએݴతʹهड़͢Δ͜ͱ͕Ͱ ͖ΔศརϥΠϒϥϦɻ • 2009೥͔ΒϚΠΫϩιϑτͷ։ൃ෦໳ʹͯRxͷ։ൃ͕࢝·ͬ ͨɻ • C#ͷLINQͱ͍͏σʔλϕʔεΛૢ࡞͢ΔϑϨʔϜϫʔΫͷه

    ड़ʹӨڹΛड͚͍ͯΔɻ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 14
  15. ଟ࠼ͳݴޠରԠ • Java: RxJava • JavaScript: RxJS • C#: Rx.NET

    • C#(Unity): UniRx • Scala: RxScala • Clojure: RxClojure • C++: RxCpp Swiftϓϩάϥϛϯάษڧձ 2016/6/30 15
  16. ଟ࠼ͳݴޠରԠ • Ruby: Rx.rb • Python: RxPY • Groovy: RxGroovy

    • JRuby: RxJRuby • Kotlin: RxKotlin • Swift: RxSwift • PHP: RxPHP Swiftϓϩάϥϛϯάษڧձ 2016/6/30 16
  17. ϓϥοτϑΥʔϜϑϨʔϜϫʔΫ • RxNetty • RxAndroid • RxCocoa Swiftϓϩάϥϛϯάษڧձ 2016/6/30 17

  18. RxSwift • 2015೥5݄͔Β։ൃ͕࢝·͍ͬͯΔޙൃͷRxϑΝϛϦʔ • Ͱ΋Githubͷελʔ͸5000Ҏ্ͱਓؾ • ·ͩऔΓࠐ·Ε͍ͯͳ͍ػೳ͕ͪΐͪ͜ΐ͋͜Δ https://github.com/ReactiveX Swiftϓϩάϥϛϯάษڧձ 2016/6/30

    18
  19. ͦͷଞͷϦΞΫςΟϒϓϩάϥϛϯάͷϑϨʔϜ ϫʔΫ • ReactiveCocoa • https://github.com/ReactiveCocoa/ReactiveCocoa • ReSwift • https://github.com/ReSwift/ReSwift

    • Swift Bond • https://github.com/SwiftBond/Bond Swiftϓϩάϥϛϯάษڧձ 2016/6/30 19
  20. iOSΞϓϦ࡞੒ͷͭΒΈ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 20

  21. ංେԽ͢ΔViewController ૿͑ଓ͚Δঢ়ଶ؅ཧ WebAPIͷඇಉظ௨৴ॲཧ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 21

  22. ! Swiftϓϩάϥϛϯάษڧձ 2016/6/30 22

  23. RxSwiftͷಛ௃ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 23

  24. RxSwiftͷಛ௃ • ίʔυΛએݴతʹ͔͚Δ • ؔ਺ܕϓϩάϥϛϯά • ඇಉظॲཧͱσʔλΠϕϯτετϦʔϜΛ؆୯ʹهड़Ͱ͖Δ ! Swiftϓϩάϥϛϯάษڧձ 2016/6/30

    24
  25. RxSwiftֶशͰͷۤ࿑ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 25

  26. RxSwiftֶशͰͷۤ࿑ • RxSwiftͰݕࡧ͢Δͱ࢖ͬͯΈͯͷײ૝ɺࣄྫ঺հ্͕Ґʹ͘ Δɻ • RxSwiftͰRxͷجຊ֓೦ͷهࣄ͕ͳ͔ͳ͔ͳ͔ͬͨɻ • ϦΞΫςΟϒϓϩάϥϛϯάͷந৅֓೦΋Ͱ͖࣮ͯͯࡍͷί ʔυͰཧղ͕ਐ·ͳ͍ɻ Swiftϓϩάϥϛϯάษڧձ

    2016/6/30 26
  27. RxSwiftֶशͰͷۤ࿑ • RxSwiftͷGithubαϯϓϧϒϩάϥϜ͸ؔ਺ϓϩάϥϛϯάͰ ૊·Εͯͯɺ಄ʹೖΒͳ͍ɻ • RxJavaͰڭ͑ͯ͘ΕΔࢿྉΛݟ͚ͭͯ֓೦Λ֮͑ΒΕͨɻ • Rxͷొ৔ਓ෺Λཧղ͢Δ·Ͱ͕ϠϚ৔ɻ Swiftϓϩάϥϛϯάษڧձ 2016/6/30

    27
  28. RxSwiftֶशͰͷۤ࿑ • ֶशίετ͸ͦͦ͜͜ߴ͍ɻ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 28

  29. Rxͷߟ͑ํ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 29

  30. Rxͷߟ͑ํ • Observable -> ετϦʔϜΛྲྀ͢ • Observers(Subscribe) -> ྲྀ͞Ε͍ͯΔ΋ͷΛݟΔ •

    Transforming and Filtering Operators -> ετϦʔϜΛมߋ • Combination Operators -> ෳ਺ͷετϦʔϜΛ૊Έ߹ΘͤΔ • Error Handling -> Τϥʔॲཧ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 30
  31. Rxͷߟ͑ํ http://reactivex.io/documentation/observable.html Swiftϓϩάϥϛϯάษڧձ 2016/6/30 31

  32. Observable Swiftϓϩάϥϛϯάษڧձ 2016/6/30 32

  33. Observable • ඇಉظͷσʔλετϦʔϜ • UI͔ΒͷΞΫγϣϯɺOS͔ΒͷNotificationɺURLͷηογϣ ϯϨεϙϯε • NextɺCompleteɺErrorͷ3ͭͷঢ়ଶΛ΋ͭ Swiftϓϩάϥϛϯάษڧձ 2016/6/30

    33
  34. ObservableΛObservable.createͰੜ੒ let disposeBag = DisposeBag() let myJust = { (element:

    String) -> Observable<String> in return Observable.create { observer in observer.on(.Next(element)) observer.on(.Completed) return NopDisposable.instance } } myJust("!") .subscribe { print($0) } .addDisposableTo(disposeBag) Swiftϓϩάϥϛϯάษڧձ 2016/6/30 34
  35. RxCocoaͰఆٛ͞Ε͍ͯΔObservable • UILabel.rx_text • UITextField.rx_text • UIButton.rx_tap • NSNotificationCenter.rx_notification •

    UISwitch.rx_value • UIView.rx_hidden Swiftϓϩάϥϛϯάษڧձ 2016/6/30 35
  36. ObservableͷNext case .Next(let value) • value = ετϦʔϜ಺ͷσʔλ Swiftϓϩάϥϛϯάษڧձ 2016/6/30

    36
  37. ObservableͷCompleted case .Completed • ετϦʔϜͷऴΘΓ • ໌ࣔతͳऴΘΓ͕ͳ͍ετϦʔϜ΋͋Δ(UIButton.rx_tap) Swiftϓϩάϥϛϯάษڧձ 2016/6/30 37

  38. ObservableͷError case .Error(let error) • error = ErrorType ܕͷ஋ •

    ετϦʔϜͷऴΘΓͰ΋͋Δ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 38
  39. Subscribe Swiftϓϩάϥϛϯάษڧձ 2016/6/30 39

  40. Subscribe • ObservableͷετϦʔϜ͔Β಺༰Λड͚औΔ • subscribeNext • subscribeCompleted • subscribeError Swiftϓϩάϥϛϯάษڧձ

    2016/6/30 40
  41. SubscribeͰϘλϯλοϓΛݕ஌ rightKeyButton.rx_tap.subscribeNext { [weak self] in self?.hoge() }.addDisposableTo(disposeBag) Swiftϓϩάϥϛϯάษڧձ 2016/6/30

    41
  42. Transforming Operators Swiftϓϩάϥϛϯάษڧձ 2016/6/30 42

  43. map • ֤ཁૉʹରͯ͠Ճ޻͢Δ http://rxmarbles.com/#map Swiftϓϩάϥϛϯάษڧձ 2016/6/30 43

  44. scan • ॳظ஋ͱ༩͑ΒΕͨॲཧͷ݁ՌΛ࣍ͷཁૉʹదԠ͠ͳ͕Β৽͍͠γʔέϯεΛ࡞Δ http://rxmarbles.com/#scan Swiftϓϩάϥϛϯάษڧձ 2016/6/30 44

  45. Filtering Operators Swiftϓϩάϥϛϯάษڧձ 2016/6/30 45

  46. filter • ৚݅ʹ߹ͬͨཁૉͷΈͷετϦʔϜΛ࡞Δ http://rxmarbles.com/#filter Swiftϓϩάϥϛϯάษڧձ 2016/6/30 46

  47. Combination Operators Swiftϓϩάϥϛϯάษڧձ 2016/6/30 47

  48. combineLatest • ̎ͭͷཁૉͷ͏ͪɺޙʹདྷͨཁૉͰ̎ͭΛ৚݅ʹ͋ͬͨՃ޻ͯ͠৽͍͠ετϦʔϜΛ࡞Δ http://rxmarbles.com/#combineLatest Swiftϓϩάϥϛϯάษڧձ 2016/6/30 48

  49. Error Handling Swiftϓϩάϥϛϯάษڧձ 2016/6/30 49

  50. retry • ࠶౓Subscribe͢Δ͜ͱͰΤϥʔΛ෮ؼ͢Δ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 50

  51. retry Swiftϓϩάϥϛϯάษڧձ 2016/6/30 51

  52. ެࣜͷαϯϓϧϓϩδΣΫτ https://github.com/ReactiveX/RxSwift • playgroundͰϝιου͕঺հ͞Ε͍ͯΔ • iOSΞϓϦͰ࣮ࡍͷΞϓϦߏங΋֮͑ΒΕΔ Swiftϓϩάϥϛϯάษڧձ 2016/6/30 52

  53. Demo https://github.com/SatoTakeshiX/RxSwiftSample Swiftϓϩάϥϛϯάษڧձ 2016/6/30 53