川で例えずに理解するObservable

 川で例えずに理解するObservable

Presented at 集まれSwift好き!Swift愛好会 vol16
https://love-swift.connpass.com/event/48820/

Ac3c0c2b458cdf0fe6a6e98a46e34322?s=128

Yoshikuni Kato

February 14, 2017
Tweet

Transcript

  1. ઒Ͱྫ͑ͣʹཧղ͢ΔObservable @yoshikuni_kato Swi$Ѫ޷ձ vol16 2017/02/14! 1

  2. Who am I? • Ճ౻༝܇ʢYoshikuni Katoʣ @yoshikuni_kato • iOSΤϯδχΞ •

    Yahoo! Japan -> Φϋί • ʮϥδ΁͐ʯ͘Μ → • Interests: ઃܭ / FRP / Coordinator PaCern / UI࣮૷ • try! SwiFʢ&ϋοΧιϯʣߦ͖·͢ʂ 2
  3. Agenda 1. ͸͡Ίʹɹ~ObservableͷΑ͋͘Δྫ͑~ 2. ઒Ͱྫ͑ͣʹཧղ͢ΔObservable 3. Hot / ColdʢReac7veSwi;ͷεεϝʣ 3

  4. ͸͡Ίʹ 4

  5. Func%onal Reac%ve Programming • ϓϩάϥϛϯάͷύϥμΠϜ • swi% libraries • Reac.veSwi%

    (Reac.veCocoa) ɹɹ˞ Reac.veCocoa͸3͔Βswi%Խ͞Ε͍ͯ·͢ʢݱࡏɺ5ʣ ɹɹ˞ 5ͰϦϙδτϦ͕੔ཧ͞ΕɺຊମʢReac.veSwi%ʣͱUIKit֦ு ʢReac.veCocoaʣͱͳΓ·ͨ͠ • RxSwi% • Rxͱݺ͹ͣʹFRPͱݺͼ͍ͨ 5
  6. Observableͱ͸ • Observable ɹ= Stream of values over 2me ɹ࣌ؒ࣠ʹԊͬͯൃੜ͢Δ஋ͷετϦʔϜ

    1 ! Observableͬͯɺʮ؍࡯Ͱ͖Δʯͬͯ ͍͏ҙຯͩΑͳ͊ɾɾɾ ɹʮ࣌ؒʯ΍ʮετϦʔϜʯͱͲ͏͍͏ ؔ܎ͳΜͩΖ͏ʁ 1 The introduc.on to Reac.ve Programming you've been missing 6
  7. Α͋͘Δྫ͑ • Observable = ઒ ! ઒ͬͯʢιϑτ΢ΣΞతʹʣԿʁ ɹ ਫ͸Ͳ͔͜ΒྲྀΕͯ͘Δͷʁ ɹ

    ྲྀΕͨਫ͸Ͳ͜ʹߦͬͯ͠·͏ͷʁ ɹ HotͱCold͕͋ΔΈ͍͚ͨͩͲɾɾɾɾ 7
  8. ྫ͑Δ͜ͱͷޭࡑ ! - ͱ͖ͬͭ΍ͦ͢͏ʹࢥ͑Δ ! - ؒҧͬͨʢ࣮૷ͱ͸ҟͳΔʣཧղΛͯ͠͠·͏Մೳੑ͕͋Δ - தͷιʔεΛͲ͏ಡΜͰ͍͍͔෼͔Βͳ͍ ιϑτ΢ΣΞతͳߏ଄Λཧղ͢Δ͜ͱ͸ॏཁ

    8
  9. ઒Ͱྫ͑ͣʹཧղ͢ΔObservable 9

  10. ஫ऍ • FRPڞ௨ͷߟ͑ํʢͷ͸ͣʣ • Reac(veSwi-ϕʔε • RxSwi-ͷํ͸ιʔεΛ͖ͪΜͱಡΊͳ͔ͬͨͷͰɺؒҧͬͯ ͍ͨΒ͢Έ·ͤΜ! • ྫͱ্ͯ͛͠Δίʔυ͸͔ͳΓ؆ུԽͨ͠΋ͷ

    10
  11. Observerύλʔϯ͕جૅ 11

  12. Observer = closure ʢΫϩʔδϟʔͷബ͍ϥούʔͱ࣮ͯ͠૷ʣ • ͲΜͳΫϩʔδϟʔ͔ʁ Event -> Void {

    event: Event in // logics when changed } 12
  13. Event // ReactiveSwift-1.0 ͷ৔߹ enum Event<Value, Error> { // RxSwiftͰ͸Errorͷࢦఆͳ͠

    case value(Value) // RxSwiftͷ৔߹͸next case failed(Error) // RxSwiftͷ৔߹͸ error(Swift.Error) case completed case interrupted // ReactiveSwiftͰ͸ͳ͠ } 13
  14. Observable = Observerͷొ࿥ઌ • Observable means "؍࡯Ͱ͖Δ΋ͷ" • Observerͷcollec0onΛ͍࣋ͬͯΔ //

    ΠϝʔδͰ͢ʂʂ class Observable { var observers: [Observer] } ※ ࣮ࡍʹ͸ɺλμͷ഑ྻͰ͸ͳ͘ɺ΋ͬͱෳࡶͳػߏͰ͢ 14
  15. observe = ObserverΛొ࿥͢Δ Observer(closure)ΛɺObservableͷObserver collec/onʹ௥Ճ͢Δ ※ Rxܥͩͱ"subscribe"ͱ͍͏໊લ 15

  16. Observable = Stream of values over 1me ͱ͸ ઌʹΫϩʔδϟʔ͕ొ࿥͞Εͯ͋Δ ʹ

    ʢObservableଆ͔ΒݟΕ͹ʣ ɹɹ೚ҙͷλΠϛϯάͰɺొ࿥͞ΕͨΫϩʔδϟʔΛ࣮ߦͰ͖Δ → ࣌ؒʹԊͬͯൃੜ͢Δ஋ΛදݱͰ͖Δ → ม਺ͦͷ΋ͷ͕ɺετϦʔϜʢΛந৅Խͨ͠΋ͷʣͱଊ͑ΒΕΔ ※ खଓ͖ܕతͳʮม਺͕ϝϞϦྖҬ(orΦϒδΣΫτ)Λ͍ࣔͯ͠Δʯ ͱ͍͏͜ͱΑΓந৅౓͕ߴ͍ 16
  17. 1 1 The introduc.on to Reac.ve Programming you've been missing

    17
  18. ʢ༨ஊʣ໋໊͕෼͔Γʹ͍͘ʁ ӳޠతɿObserver observe observable. (S-V-O) ίʔυɿobservable.observe(observer) ӳޠతͳҙຯͱ࣮ࡍͷڍಈ͕൓ର ʢຊདྷɺobservable.observed(by: observer)ɹͳͲʣ Reac%veSwi*Ͱ͸ଟগٞ࿦ʹͳ͍ͬͯΔ

    2 2 Rename Signal.observe(_:) 18
  19. operator = observable -> observableͷม׵ • map / filter /

    flatMap / combineLatestͳͲͳͲɾɾɾ • RxMarble / RACMarbleΛࢀর 19
  20. ʮ೚ҙͷλΠϛϯάͰ࣮ߦ͢Δʯͷ࢓૊Έ Reac%veSwi*ͷ৔߹ • Signal(HotObservable)͸innerObserverͱηοτͰ࡞ΒΕɺinnerObserver͸Signal΁ͷࢀরΛ࣋ͭ • innerObserverΛ࣮ߦ͢ΔʢobserverʹΠϕϯτΛ౉͢ʣ -> SignalͷobserversΛશͯʹΠϕϯτΛ౉͢ // Πϝʔδ

    let innerObserver = { event in for observer in signal.observers { observer.send(event) } } • operator(map/filter/...)͸ɺ͜ͷinnerObserverΛɺલͷSignalͷobserverʹొ࿥͢ΔΑ͏ͳܗ 20
  21. Hot / Cold ʢReac%veSwi*ͷεεϝʣ 21

  22. Hot / Cold • Hot / Coldʢ֓೦ʣ͔ΒೖΔͱ෼͔Γʹ͍͘ • Reac.veSwi3ͷߟ͑ํ͕ࢀߟʹͳΔ 22

  23. Reac%veSwi*(Cocoa)ʹ͓͚Δ Hot/Cold • Reac&veCocoa2·ͰɿHot/Cold྆ํಉ͡ܕʢࠓͷRxͱಉ༷ʣ • Reac&veCocoa3Ҏ߱ɿHot/ColdΛܕͰ෼཭ • Hot : Signal

    • Cold: SignalProducer ͭ·ΓʮHotObservable ͱ ͦͷੜ੒ثʯͱཧղͰ͖Δ 23
  24. Signal / SignalProducerʹ͍ͭͯ Qiitaʹ࠷ۙهࣄΛॻ͖·ͨ͠ Reac%veSwi* جຊཁૉͷ࢖͍ํ Signal / SignalProducerฤ 24

  25. Reac%veSwi* vs RxSwi* 25

  26. Reac%veSwi* vs RxSwi* ! • Hot / Coldͷѻ͍͕෼͔Γ΍͍͢ • Τϥʔͷܕ͕ࢦఆͰ͖Δ

    • ֮͑Δܕ͕গͳ͍ʢSignal/SignalProducer/Property/Ac7on/Disposable͘Β ͍ʣ • தͷ࣮૷ͷɺϑΝΠϧ਺͕গͳ͘ɺಡΈ΍ͦ͢͏ͳҹ৅ʢʹͳͬͨʣ • Carthageͷ࣮૷ʹ࢖༻͞Ε͍ͯΔ 26
  27. Reac%veSwi*ͷϑΝΠϧߏ੒ 27

  28. Reac%veSwi* vs RxSwi* ! • ೔ຊޠͷυΩϡϝϯτ͕গͳ͍ • ݕࡧͯ͠΋ɺݹ͍هࣄ(Obj-C)͕ώοτ͕ͪ͠ • όʔδϣϯʹΑΔഁյతมߋ͕ଟ͍

    • ίϛϡχςΟ͕ऑΊʢRealmͱͷ࿈ܞͳͲʣ • ΦϑΟγϟϧͷαϯϓϧͳͲ͕ͳ͍ 28
  29. ·ͱΊ 29

  30. ·ͱΊ • FRP͸Φϒβʔόʔύλʔϯ͕ࠜఈʹ͋Δ • ObservableͰΠϕϯτετϦʔϜͦͷ΋ͷΛදͤΔ • ιϑτ΢ΣΞతͳߏ଄Λཧղ͢Δ͜ͱ͸ॏཁ • Reac.veSwi2ʢReac.veCocoaʣ΋࢖ͬͯΈ͍ͯͩ͘͞! 30

  31. iOSΤϯδχΞืूத • ࣄۀɿUX/UIσβΠϯ & ։ൃ ɹ ʢࠓͷͱ͜Ζʣ৽نडୗ։ൃϝΠϯ • ࣾһɿ20໊ڧʢiOSΤϯδχΞ 3໊ʣ

    • ΋ͪΖΜSwi/ • ։ൃͷࣗ༝౓ߴ͍ • try ! Swi/ (16/17) / iOSDC εϙϯαʔ • UI࣮૷޷͖ͳਓɺઃܭ޷͖ͳਓٻΉ 31
  32. Thank you! 32

  33. ิ଍ 33

  34. ࢲ͕Reac%veSwi*Λ࢖͍࢝ΊͨܦҢ • ࠷ॳʢ2016/3݄͝Ζʣ͸RxSwi-ΛʢྲྀߦΓͰʣษڧ • Reac1veSwi-Meetup@WantedlyͰReac1veCocoaͷ࿩Λฉ͖ɺ ઃܭ͕ྑͦ͞͏ͩͱࢥ͍ɺ࢖͍࢝ΊΔ • @inamy͞Μɺ@ikesyo͞ΜɺNick͞Μ • ࠓ͸ޙչ͍ͯ͠ͳ͍

    34
  35. ࢀߟ • Reac&veCocoa/Reac&veSwi. • Reac&veCocoa/Reac&veCocoa • RACCommunity • Reac&veX/RxSwi. •

    Reac&ve Swi. Meetup @WantedlyΛ։࠵͠·ͨ͠ʂ • @ikesyo͞Μͷࢿྉ • Reac&veSwi. جຊཁૉͷ࢖͍ํ Signal / SignalProducerฤ 35