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

川で例えずに理解するObservable

 川で例えずに理解するObservable

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

Yoshikuni Kato

February 14, 2017
Tweet

More Decks by Yoshikuni Kato

Other Decks in Programming

Transcript

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

    Yahoo! Japan -> Φϋί • ʮϥδ΁͐ʯ͘Μ → • Interests: ઃܭ / FRP / Coordinator PaCern / UI࣮૷ • try! SwiFʢ&ϋοΧιϯʣߦ͖·͢ʂ 2
  2. 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
  3. Observableͱ͸ • Observable ɹ= Stream of values over 2me ɹ࣌ؒ࣠ʹԊͬͯൃੜ͢Δ஋ͷετϦʔϜ

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

    ྲྀΕͨਫ͸Ͳ͜ʹߦͬͯ͠·͏ͷʁ ɹ HotͱCold͕͋ΔΈ͍͚ͨͩͲɾɾɾɾ 7
  5. 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
  6. Observable = Observerͷొ࿥ઌ • Observable means "؍࡯Ͱ͖Δ΋ͷ" • Observerͷcollec0onΛ͍࣋ͬͯΔ //

    ΠϝʔδͰ͢ʂʂ class Observable { var observers: [Observer] } ※ ࣮ࡍʹ͸ɺλμͷ഑ྻͰ͸ͳ͘ɺ΋ͬͱෳࡶͳػߏͰ͢ 14
  7. Observable = Stream of values over 1me ͱ͸ ઌʹΫϩʔδϟʔ͕ొ࿥͞Εͯ͋Δ ʹ

    ʢObservableଆ͔ΒݟΕ͹ʣ ɹɹ೚ҙͷλΠϛϯάͰɺొ࿥͞ΕͨΫϩʔδϟʔΛ࣮ߦͰ͖Δ → ࣌ؒʹԊͬͯൃੜ͢Δ஋ΛදݱͰ͖Δ → ม਺ͦͷ΋ͷ͕ɺετϦʔϜʢΛந৅Խͨ͠΋ͷʣͱଊ͑ΒΕΔ ※ खଓ͖ܕతͳʮม਺͕ϝϞϦྖҬ(orΦϒδΣΫτ)Λ͍ࣔͯ͠Δʯ ͱ͍͏͜ͱΑΓந৅౓͕ߴ͍ 16
  8. operator = observable -> observableͷม׵ • map / filter /

    flatMap / combineLatestͳͲͳͲɾɾɾ • RxMarble / RACMarbleΛࢀর 19
  9. ʮ೚ҙͷλΠϛϯάͰ࣮ߦ͢Δʯͷ࢓૊Έ 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
  10. Reac%veSwi* vs RxSwi* ! • Hot / Coldͷѻ͍͕෼͔Γ΍͍͢ • Τϥʔͷܕ͕ࢦఆͰ͖Δ

    • ֮͑Δܕ͕গͳ͍ʢSignal/SignalProducer/Property/Ac7on/Disposable͘Β ͍ʣ • தͷ࣮૷ͷɺϑΝΠϧ਺͕গͳ͘ɺಡΈ΍ͦ͢͏ͳҹ৅ʢʹͳͬͨʣ • Carthageͷ࣮૷ʹ࢖༻͞Ε͍ͯΔ 26
  11. iOSΤϯδχΞืूத • ࣄۀɿUX/UIσβΠϯ & ։ൃ ɹ ʢࠓͷͱ͜Ζʣ৽نडୗ։ൃϝΠϯ • ࣾһɿ20໊ڧʢiOSΤϯδχΞ 3໊ʣ

    • ΋ͪΖΜSwi/ • ։ൃͷࣗ༝౓ߴ͍ • try ! Swi/ (16/17) / iOSDC εϙϯαʔ • UI࣮૷޷͖ͳਓɺઃܭ޷͖ͳਓٻΉ 31
  12. ࢀߟ • Reac&veCocoa/Reac&veSwi. • Reac&veCocoa/Reac&veCocoa • RACCommunity • Reac&veX/RxSwi. •

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