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

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

yimajo
November 22, 2020

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

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

yimajo

November 22, 2020
Tweet

More Decks by yimajo

Other Decks in Programming

Transcript

  1. TCAͰViewStoreͷStateʹ


    KeyPath DynamicMemberLookup͕ɹ


    ࢖ΘΕͯΔ݅
    @yimajo 2020/11/22 #fa_ios
    iOSΞϓϦ։ൃͷͨΊͷFunctional Architecture৘ใڞ༗ձ2

    View Slide

  2. TCA͸KeyPathʹΑΔ
    DynamicMemberLookup࢖ͬͯΜͷ͓΋͢
    Εʔʂ


    ͜Ε͸͍͍ײ͡ͷ࢖͍ํ΍ʔ

    View Slide

  3. ຊ౰ʹͦ͏͔ʁ


    ඞવੑͷ͋Δ࢖͍ํ͔ʁ

    View Slide

  4. ͍΍͍΍͍΍ɺ͍͍Ͱ͠ΐɻ


    Swift 4͘Β͍͔Βͷ KeyPath ɺ


    ͦͯ͠ Swift 4.2 ͔Βͷ Dynamic Member Lookupɺ


    ͜ΕΛ্ख͍͜ͱ૊Έ߹Θͤͨ Swift 5.1 ͔Βͷ


    Key Path ʹΑΔ Dynamic Member Lookup ΍Μɺ


    ͜ΕΛ࢖ͬͯΔͱؒ઀తʹͰ΋StateʹܕͰΞΫηε
    Ͱ͖ͯؾ࣋ͪ͑ʔΜ΍ɺͦΕҎ্ͷԿ͕͋Μͷ΍ʔʁ

    View Slide

  5. ͜Ε͸ͩΊͩɺௐ΂ͯ
    ߟ͑௚ͯ͠ΈΑ͏͔…

    View Slide

  6. ࠓճͷ࿩ͷ֓ཁ

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  13. ۩ମྫ

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  25. 4XJGU6*7JFX
    4UBUF )BOEMF

    View Slide

  26. ࣮ࡍ


    TCA Ͱ KeyPath Member Lookup


    ͨ͘͠ͳΔؾ࣋ͪ΋෼͔Δ͕


    ͳͯ͘΋ྑ͍ؾ΋͢Δ

    View Slide

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

    View Slide

  28. TCAͰUIKitͰͷ


    Χ΢ϯλʔαϯϓϧ
    … লུ

    View Slide

  29. TCAͰUIKitͰͷ


    Χ΢ϯλʔαϯϓϧ
    … লུ

    View Slide

  30. TCAͰUIKitͰͷ


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

    View Slide

  31. TCAͰUIKitͰͷ


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

    View Slide

  32. TCAͰUIKitͰͷ


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


    State ͕ Publisher ͰऔಘͰ͖Δ

    View Slide

  33. ViewStoreΈͯΈΔ

    View Slide

  34. ViewStoreΈͯΈΔ
    • publisher:
    StorePublisher

    View Slide

  35. ViewStoreΈͯΈΔ
    • publisher:
    StorePublisher
    StorePublisher Λ


    ͞Βʹৄ͘͠஌Γ͍ͨ

    View Slide

  36. StorePublisherΈͯΈΔ

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide


  41. self.viewStore.publisher.count


    .assign(to: \.text, on: countLabel)


    .store(in: &self.cancellables)
    ͭ·Γ …

    View Slide


  42. self.viewStore.publisher.count


    .assign(to: \.text, on: countLabel)


    .store(in: &self.cancellables)
    ࠨͷΑ͏ʹࣗલͰ .publisher ʹ .map Λॻ͔ͳ͍͍ͯ͘ʂ
    ͭ·Γ …

    View Slide


  43. self.viewStore.publisher.count


    .assign(to: \.text, on: countLabel)


    .store(in: &self.cancellables)
    ࠨͷΑ͏ʹࣗલͰ .publisher ʹ .map Λॻ͔ͳ͍͍ͯ͘ʂ
    KeyPath Member Lookup ࢖ͬͯলུͰ͖Δʢӈʣɻ
    ͭ·Γ …

    View Slide

  44. View Slide

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

    View Slide


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

    View Slide


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

    View Slide


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

    View Slide

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


    StorePublisherͳ count ฦ͢
    ϓϩύςΟͷ
    ஋͕ཉ͍͠
    1VCMJTIFSΛ
    TJOLͯ͠
    Ͳ͏ʹ͔͍ͨ͠

    View Slide

  50. ͓ΘΓʹ: ٞ࿦ͷϙΠϯτ
    • KeyPath Member Lookup ͳͯ͘΋ྑ͍ͱ͸ࢥ͏͚Ͳ΋


    • Publisherͱͯ͠ཉ͍͠৔߹ͷΠϯλϑΣʔεͰ͸৑௕͞Λল͚ͯΔ


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

    View Slide