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

TCAでViewStoreにKeyPath DynamicMemberLookupが使われてる件

7d1521d950d9bc5697868830cb04ff2f?s=47 yimajo
November 22, 2020

TCAでViewStoreにKeyPath DynamicMemberLookupが使われてる件

iOSアプリ開発のためのFunctional Architecture情報共有会2 の発表資料です。
https://connpass.com/event/194239/

7d1521d950d9bc5697868830cb04ff2f?s=128

yimajo

November 22, 2020
Tweet

Transcript

  1. TCAͰViewStoreͷStateʹ KeyPath DynamicMemberLookup͕ɹ ࢖ΘΕͯΔ݅ @yimajo 2020/11/22 #fa_ios iOSΞϓϦ։ൃͷͨΊͷFunctional Architecture৘ใڞ༗ձ2

  2. TCA͸KeyPathʹΑΔ DynamicMemberLookup࢖ͬͯΜͷ͓΋͢ Εʔʂ ͜Ε͸͍͍ײ͡ͷ࢖͍ํ΍ʔ

  3. ຊ౰ʹͦ͏͔ʁ ඞવੑͷ͋Δ࢖͍ํ͔ʁ

  4. ͍΍͍΍͍΍ɺ͍͍Ͱ͠ΐɻ Swift 4͘Β͍͔Βͷ KeyPath ɺ ͦͯ͠ Swift 4.2 ͔Βͷ Dynamic

    Member Lookupɺ ͜ΕΛ্ख͍͜ͱ૊Έ߹Θͤͨ Swift 5.1 ͔Βͷ Key Path ʹΑΔ Dynamic Member Lookup ΍Μɺ ͜ΕΛ࢖ͬͯΔͱؒ઀తʹͰ΋StateʹܕͰΞΫηε Ͱ͖ͯؾ࣋ͪ͑ʔΜ΍ɺͦΕҎ্ͷԿ͕͋Μͷ΍ʔʁ
  5. ͜Ε͸ͩΊͩɺௐ΂ͯ ߟ͑௚ͯ͠ΈΑ͏͔…

  6. ࠓճͷ࿩ͷ֓ཁ

  7. ࠓճͷ࿩ͷ֓ཁ • TCAͰ͸KeyPathʹΑΔDynamic Member LookupʢҎ߱ KeyPath Member Lookupʣ ͕࢖ΘΕ͍ͯΔ

  8. ࠓճͷ࿩ͷ֓ཁ • TCAͰ͸KeyPathʹΑΔDynamic Member LookupʢҎ߱ KeyPath Member Lookupʣ ͕࢖ΘΕ͍ͯΔ •

    SwiftUI.View ͔Β State ͷ஋ΛݟΔͱ͖ͳͲ
  9. ࠓճͷ࿩ͷ֓ཁ • TCAͰ͸KeyPathʹΑΔDynamic Member LookupʢҎ߱ KeyPath Member Lookupʣ ͕࢖ΘΕ͍ͯΔ •

    SwiftUI.View ͔Β State ͷ஋ΛݟΔͱ͖ͳͲ • ҙຯ͋Δͷʁ௕ॴ͸͋Δͷʁඞવੑ͸ʁ
  10. ࠓճͷ࿩ͷ֓ཁ • TCAͰ͸KeyPathʹΑΔDynamic Member LookupʢҎ߱ KeyPath Member Lookupʣ ͕࢖ΘΕ͍ͯΔ •

    SwiftUI.View ͔Β State ͷ஋ΛݟΔͱ͖ͳͲ • ҙຯ͋Δͷʁ௕ॴ͸͋Δͷʁඞવੑ͸ʁ • ͜Ε͸ SwiftUI.View ͚ͩݟͯΔͱҙຯ΍ඞવੑ͸෼͔ΓͮΒ͍͔΋
  11. ࠓճͷ࿩ͷ֓ཁ • TCAͰ͸KeyPathʹΑΔDynamic Member LookupʢҎ߱ KeyPath Member Lookupʣ ͕࢖ΘΕ͍ͯΔ •

    SwiftUI.View ͔Β State ͷ஋ΛݟΔͱ͖ͳͲ • ҙຯ͋Δͷʁ௕ॴ͸͋Δͷʁඞવੑ͸ʁ • ͜Ε͸ SwiftUI.View ͚ͩݟͯΔͱҙຯ΍ඞવੑ͸෼͔ΓͮΒ͍͔΋ • TCAͰͳ͍ઃܭ΋ҰԠৼΓฦΔ
  12. ࠓճͷ࿩ͷ֓ཁ • TCAͰ͸KeyPathʹΑΔDynamic Member LookupʢҎ߱ KeyPath Member Lookupʣ ͕࢖ΘΕ͍ͯΔ •

    SwiftUI.View ͔Β State ͷ஋ΛݟΔͱ͖ͳͲ • ҙຯ͋Δͷʁ௕ॴ͸͋Δͷʁඞવੑ͸ʁ • ͜Ε͸ SwiftUI.View ͚ͩݟͯΔͱҙຯ΍ඞવੑ͸෼͔ΓͮΒ͍͔΋ • TCAͰͳ͍ઃܭ΋ҰԠৼΓฦΔ • UIKit ͔Β࢖͏৔߹Λ஌ͬͯΔͱඞવੑ͕͋ΔΑ͏ʹࢥ͑Δ
  13. ۩ମྫ

  14. TCA ͷ SwiftUI Χ΢ϯλʔαϯϓϧ

  15. TCA ͷ SwiftUI Χ΢ϯλʔαϯϓϧ

  16. TCA ͷ SwiftUI Χ΢ϯλʔαϯϓϧ • SwiftUI͕storeΛอ࣋

  17. TCA ͷ SwiftUI Χ΢ϯλʔαϯϓϧ • SwiftUI͕storeΛอ࣋ • store͔ΒviewStoreม਺औΓग़͠

  18. TCA ͷ SwiftUI Χ΢ϯλʔαϯϓϧ • SwiftUI͕storeΛอ࣋ • store͔ΒviewStoreม਺औΓग़͠ • viewStoreܦ༝ͰcountʹΞΫηε

  19. 4XJGU6*7JFX 4UPSF4UBUF "DUJPO 4UBUF ViewStore

  20. bow-swift/bow-arch ൺֱͱͯ͠

  21. bow-swift/bow-arch ൺֱͱͯ͠

  22. bow-swift/bow-arch • SwiftUI.View͕stateͱhandleΛอ࣋ ൺֱͱͯ͠

  23. bow-swift/bow-arch • SwiftUI.View͕stateͱhandleΛอ࣋ • handle͕Stateͷૢ࡞ ൺֱͱͯ͠

  24. bow-swift/bow-arch • SwiftUI.View͕stateͱhandleΛอ࣋ • handle͕Stateͷૢ࡞ • ͍ΘΏΔReducer ൺֱͱͯ͠

  25. 4XJGU6*7JFX 4UBUF )BOEMF

  26. ࣮ࡍ TCA Ͱ KeyPath Member Lookup ͨ͘͠ͳΔؾ࣋ͪ΋෼͔Δ͕ ͳͯ͘΋ྑ͍ؾ΋͢Δ

  27. UIKitͰͷTCAͷ࢖͍ํΛݟͯΈΔ

  28. TCAͰUIKitͰͷ Χ΢ϯλʔαϯϓϧ … লུ

  29. TCAͰUIKitͰͷ Χ΢ϯλʔαϯϓϧ … লུ

  30. TCAͰUIKitͰͷ Χ΢ϯλʔαϯϓϧ • viewStore.publisher Ͱ $0.count … লུ

  31. TCAͰUIKitͰͷ Χ΢ϯλʔαϯϓϧ • viewStore.publisher Ͱ $0.count • ͦͷ஋ΛUITextLabelͷtextʹ … লུ

  32. TCAͰUIKitͰͷ Χ΢ϯλʔαϯϓϧ • viewStore.publisher Ͱ $0.count • ͦͷ஋ΛUITextLabelͷtextʹ … লུ

    ͭ·Γ State ͕ Publisher ͰऔಘͰ͖Δ
  33. ViewStoreΈͯΈΔ

  34. ViewStoreΈͯΈΔ • publisher: StorePublisher<State>

  35. ViewStoreΈͯΈΔ • publisher: StorePublisher<State> StorePublisher Λ ͞Βʹৄ͘͠஌Γ͍ͨ

  36. StorePublisherΈͯΈΔ

  37. StorePublisherΈͯΈΔ • StorePublisher͸Publisherʹ४ڌͯ͠ ͍ΔͷͰStateΛετϦʔϜʹྲྀͤΔ

  38. StorePublisherΈͯΈΔ • StorePublisher͸Publisherʹ४ڌͯ͠ ͍ΔͷͰStateΛετϦʔϜʹྲྀͤΔ

  39. StorePublisherΈͯΈΔ • StorePublisher͸Publisherʹ४ڌͯ͠ ͍ΔͷͰStateΛετϦʔϜʹྲྀͤΔ • ͞Βʹ subscript Ͱmapͯ͠Δ

  40. StorePublisherΈͯΈΔ • StorePublisher͸Publisherʹ४ڌͯ͠ ͍ΔͷͰStateΛετϦʔϜʹྲྀͤΔ • ͞Βʹ subscript Ͱmapͯ͠Δ ͭ·ΓɺStorePublisherʹରͯ͠΋ KeyPath

    Member Lookup ͯ͠ map ͠ Publisher Λฦͯ͠Δ
  41. → self.viewStore.publisher.count .assign(to: \.text, on: countLabel) .store(in: &self.cancellables) ͭ·Γ …

  42. → self.viewStore.publisher.count .assign(to: \.text, on: countLabel) .store(in: &self.cancellables) ࠨͷΑ͏ʹࣗલͰ .publisher

    ʹ .map Λॻ͔ͳ͍͍ͯ͘ʂ ͭ·Γ …
  43. → self.viewStore.publisher.count .assign(to: \.text, on: countLabel) .store(in: &self.cancellables) ࠨͷΑ͏ʹࣗલͰ .publisher

    ʹ .map Λॻ͔ͳ͍͍ͯ͘ʂ KeyPath Member Lookup ࢖ͬͯলུͰ͖Δʢӈʣɻ ͭ·Γ …
  44. None
  45. \\\ ετϦʔϜΛѻ͏ͱ͖ State ͱ͍͏ม਺͸ݟ͑ͳͯ͘ྑͦ͞͏ ///

  46. ↓ \\\ ετϦʔϜΛѻ͏ͱ͖ State ͱ͍͏ม਺͸ݟ͑ͳͯ͘ྑͦ͞͏ ///

  47. ↓ ετϦʔϜΛѻΘͣ State ͷϓϩύςΟ͕ཉ͍͠ͱ͖΋ɺ \\\ ετϦʔϜΛѻ͏ͱ͖ State ͱ͍͏ม਺͸ݟ͑ͳͯ͘ྑͦ͞͏ ///

  48. ↓ ετϦʔϜΛѻΘͣ State ͷϓϩύςΟ͕ཉ͍͠ͱ͖΋ɺ State ͱ͍͏ม਺͸ݟ͑ͳ͍΄͏͕ରশੑ͕͋Δ…͔΋… \\\ ετϦʔϜΛѻ͏ͱ͖ State ͱ͍͏ม਺͸ݟ͑ͳͯ͘ྑͦ͞͏

    ///
  49. 7JFX4UPSF 4UBUF viewStore.count ͍ͩ͘͞ state.count: Int ฦ͢ viewStore.publisher.count ͍ͩ͘͞ StorePublisher<State>͔Β

    StorePublisher<Int>ͳ count ฦ͢ ϓϩύςΟͷ ஋͕ཉ͍͠ 1VCMJTIFSΛ TJOLͯ͠ Ͳ͏ʹ͔͍ͨ͠
  50. ͓ΘΓʹ: ٞ࿦ͷϙΠϯτ • KeyPath Member Lookup ͳͯ͘΋ྑ͍ͱ͸ࢥ͏͚Ͳ΋ • Publisherͱͯ͠ཉ͍͠৔߹ͷΠϯλϑΣʔεͰ͸৑௕͞Λল͚ͯΔ •

    StateͷϓϩύςΟ͕΄͍͠ͱ͖΋ɺKeyPath Member LookupΛ ࢖ͬͨ΄͏͕ରশੑ΍౷Ұײ͕͋Δͷ͸෼͔Δ