Getting Started Combine

Getting Started Combine

Combine をはじめよう

Mobile Act ONLINE #1
https://mobileact.connpass.com/event/181370/

0b26590a0a8b0f1da140ed5de9b68379?s=128

USAMI Kosuke

July 22, 2020
Tweet

Transcript

  1. Combine Λ͸͡ΊΑ͏ USAMI Kosuke 1/23

  2. ࣗݾ঺հ • Ӊࠤݟ ެี • ϑΣϯϦϧגࣜձࣾʢେࡕຊࣾʣiOS ΤϯδχΞ • Mobile Act

    ओ࠵ • iOSDC ݪߘ४උத 2/23
  3. Combine ͱ͸ 3/23

  4. Combine ͱ͸ • Reactive Programming ͷϑϨʔϜϫʔΫ • RxSwift ΍ ReactiveSwift

    ͱಉྨ • ඇಉظॲཧΛॊೈʹهड़Ͱ͖Δ • Ϟσϧͱ UI ͷόΠϯσΟϯάΛهड़Ͱ͖Δ 4/23
  5. ࡢ೥ൃද͞Εͯ࿩୊ʹͳͬͨ • Combine ͸ࡢ೥ͷ WWDC19 Ͱൃද͞Εͨ • ࿩୊ʹ͸ͳͬͨ΋ͷͷɺiOS 13 ͰͷΈ࢖༻Մೳ

    • → ࣮ࡍͷ։ൃʹద༻͠ʹ͔ͬͨ͘ • ͔͠͠ɺ1೥ͨͬͨݱࡏɺঢ়گ͸ҟͳΔ 5/23
  6. iOS ΞϓϦͷ։ൃࣄ৘ • ཧ૝తʹ͸࠷৽ OS ͷΈΛαϙʔτ͍ͨ͠ • ͔࣮͠͠ࡍ͸ɺগ͠લͷ OS ͷαϙʔτΛཁ๬͞ΕΔ

    • ࠷৽ OS ͱͦͷ1ͭલͷ OS ·Ͱͷαϙʔτ͕ݱ࣮࿏ઢ • iOS 14 ϦϦʔεޙ͸ iOS 13 ʙ 14 ରԠ͸ݱ࣮త 6/23
  7. Combine ͷݱঢ় • ࡢ೥࣌఺Ͱɺطʹ͋Δఔ౓੒ख़ͨ͠ϑϨʔϜϫʔΫͱ͍͑Δ • Reactive Programming ͸طʹ৽͍͠΋ͷͰ͸ͳ͍ • ࠓ೥ͷ௥Ճػೳ͸΄ͱΜͲͳ͍

    7/23
  8. ଞͷϑϨʔϜϫʔΫͱͷൺֱ • طʹ Reactive Programming ϑϨʔϜϫʔΫ͸͋Δ • RxSwift : ReactiveX

    ܥ • ReactiveSwift : Swift ͷྑ͞Λ׆͔͍ͯ͠Δ • Combine ͷར఺ • αʔυύʔςΟ੡Ͱͳ͘ඪ४Ͱ͋Δ 8/23
  9. ͳͥɺ͍· Combine Λ͸͡ΊΔͷ͔ • λΠϛϯά͕ͪΐ͏Ͳྑ͍ • iOS 13 Ҏ߱Λର৅ͱ͢Δ͜ͱ͕ݱ࣮తʹՄೳʹͳ͖ͬͯͨ •

    ͋Δఔ౓੒ख़ͨ͠ϑϨʔϜϫʔΫͰ͋Γ҆৺ͯ͠࢖͑Δ • ඪ४ϑϨʔϜϫʔΫͳͷͰαʔυύʔςΟґଘ͠ͳ͍ 9/23
  10. UIKit / SwiftUI • Combine ͸ͲͪΒͱ΋૊Έ߹ΘͤΒΕΔ൚༻తͳ΋ͷ • ࠓ͸ UIKit ͷ૊Έ߹Θ͕ͤݱ࣮త

    • SwiftUI ͸ iOS 13 ରԠ͕ඞཁͳঢ়گͰ͸ػೳෆ଍ؾຯ • iOS 14 Ͱ͸େ͖͘վળ͍ͯ͠Δ 10/23
  11. Combine Λֶ΅͏ 11/23

  12. Combine Λֶ΅͏ • WWDC19 ͷηογϣϯ • ࠓͳΒ೔ຊޠࣈນ΋͋Δ • ༷ʑͳਓ͕هࣄ΍ॻ੶Λॻ͍͍ͯΔ •

    ࡢ೥ొ৔Ҏ߱Ͱ஫໨౓͕ߴ͍͜ͱ͕෼͔Δ • ଞͷϑϨʔϜϫʔΫͱͷ༻ޠͷҧ͍ʹ͸஫ҙ 12/23
  13. ͳΜ͔ͩͪΐͬͱ೉ͦ͠͏ɾɾɾʁ • ͍͖ͳΓશ෦΍Ζ͏ͱͯ͠΋೉͍͔͠΋͠Εͳ͍ • Ͱ͖Δ͚ͩ؆୯ͳαϯϓϧίʔυΛߟ͑ͯΈ·͠ΐ͏ 13/23

  14. ྫ : NotificationCenter let cancellable = NotificationCenter.default .publisher(for: NSControl.textDidChangeNotification, object:

    textField) .sink(receiveValue: { /* Πϕϯτॲཧ */ }) 14/23
  15. ྫ : URLSession let cancellable = URLSession.shared .dataTaskPublisher(for: request) .sink(receiveValue:

    { /* Πϕϯτॲཧ */ }) 15/23
  16. ڞ௨͢Δܗ͕͋Δ let cancellable = ԿΒ͔ͷΦϒδΣΫτ .publisher(for: ...) .sink(receiveValue: { /*

    Πϕϯτॲཧ */ }) 16/23
  17. جຊܗ • Publisher ͕ΠϕϯτΛૹग़͢Δ • sink ͰΠϕϯτΛॲཧ͢Δ • sink ͸

    Subscriber Λੜ੒͢Δ 17/23
  18. Combine Λ࢖͏ҙຯ • ҎԼͷΑ͏ͳඇಉظॲཧΛ͢΂ͯ Combine Ͱॻ͚Δ • Target / Action

    • Notification Center • URL Session • Key-Value Observing • Callback 18/23
  19. Combine ͷ3ͭͷجຊཁૉ • Publisher : ΠϕϯτΛૹग़͢Δ • Operator • Subscriber

    : ΠϕϯτΛॲཧ͢Δ 19/23
  20. Operator Λ࢖͑Δͷ͕ڧΈ • ෳ਺ͷΠϕϯτΛ؂ࢹͯ͠ॲཧ͢ΔɺͳͲ͕؆୯ʹͳΔ • Zip • CombineLatest • ͳͲ

    20/23
  21. ิ଍ : UIControl ͷରԠ • UIButton ͳͲͷ Publisher ͕ඪ४Ͱ͸༻ҙ͞Ε͍ͯͳ͍ •

    ͜Ε͸ඪ४Ͱཉ͔ͬͨ͠ͱ͜Ζ͕ͩɾɾɾ • ΧελϜ Publisher Λ࣮૷ͯ͠ରԠ͢Δ • ࢀߟ : ʲSwiftʳCustom PublisherΛ࡞੒ͯ͠Combineϑ ϨʔϜϫʔΫͷಈ͖ΛֶͿ - @shiz ͞Μͷ Qiita هࣄ 21/23
  22. ิ଍ : UIControl ͷରԠ ΧελϜ Publisher Λ࣮૷͢Ε͹ҎԼͷΑ͏ͳ͜ͱ͕Ͱ͖Δ let cancellable =

    button .publisher(for: ...) .sink(receiveValue: { /* Πϕϯτॲཧ */ }) 22/23
  23. ·ͱΊ • Combine ͸͍·࢝ΊΔͷ͕ͪΐ͏Ͳྑ͍ • ͺͬͱݟͰ͸೉ͦ͠͏͕ͩجຊ͸؆୯ 23/23