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

Swift Concurrency時代のリアクティブプログラミングの基礎理解

Swift Concurrency時代のリアクティブプログラミングの基礎理解

ばんじゅん🍓

September 11, 2022
Tweet

More Decks by ばんじゅん🍓

Other Decks in Programming

Transcript

  1. © ZOZO, Inc. 2 גࣜձࣾZOZO ςοΫϦʔυ ZOZOTOWN iOS ͹Μ͡ΎΜ! /

    @banjun -PWF-JWFS NBD04 $SFBUF-JWF1IPUPTGSPN.PWJFT 'MJDL4,, J04 +BQBOFTF$VTUPN,FZCPBSE&YUFOTJPO ݸਓϓϩδΣΫτɾ1PEDBTU ͖ͷ͜ΔΤϑΤϜX!USFCZ͖ͷ͜Δ
  2. © ZOZO, Inc. https://zozo.jp/ 3 • ϑΝογϣϯEC • 1,500Ҏ্ͷγϣοϓɺ8,500Ҏ্ͷϒϥϯυͷऔΓѻ͍ •

    ৗ࣌90ສ఺Ҏ্ͷ঎඼ΞΠςϜ਺ͱຖ೔ฏۉ2,600఺Ҏ্ͷ৽ண
 ঎඼Λܝࡌʢ2022೥6݄຤࣌఺ʣ • ϒϥϯυݹணͷϑΝογϣϯκʔϯʮZOZOUSEDʯ΍ ίεϝઐ໳ϞʔϧʮZOZOCOSMEʯɺۺͷઐ໳Ϟʔϧ ʮZOZOSHOESʯɺϥάδϡΞϦʔˍσβΠφʔζκʔϯ ʮZOZOVILLAʯΛల։ • ଈ೔഑ૹαʔϏε • ΪϑτϥοϐϯάαʔϏε • πέ෷͍ ͳͲ
  3. © ZOZO, Inc. 12 ࣌୅എܠͱղܾ͍ͨ͠՝୊ 4XJGUͰJ04ΞϓϦΛॻ͚ΔΑ͏ʹͳͬͨ ઃܭύλʔϯ͕ྲྀߦΓɺ31 ϦΞΫςΟϒϓϩάϥϛϯά ΋ ྲྀߦͬͨ

    4XJGU6*ͱ$PNCJOF͕ग़ͨ 4XJGU$PODVSSFODZ͕ग़ͨ Θͨͨͪ͠͸Ͳ͜΁޲͔͑͹͍͍ͷ͔ʁ    
  4. © ZOZO, Inc. 13 ϦΞΫςΟϒϓϩάϥϛϯά(RP)ͱ
 Swift Concurrency 4XJGU$PODVSSFODZ ϦΞΫςΟϒϓϩάϥϛϯά ඇಉظϓϩάϥϛϯά

    σʔλετϦʔϜ ϥΠϒϥϦͰ͋Δ ݴޠͰ͋Δ ʮ͍ΘΏΔRPʯΛ ࣮ݱ͍ͨ͠ ฒߦॲཧΛ࣮ݱ͍ͨ͠ ڞ௨఺
  5. © ZOZO, Inc. 14 ʮ͍ΘΏΔRPʯ ※͝஫ҙ 31ͷࢦ͢΋ͷ͸ॾઆ͋ΔͷͰɺ͜ͷεϥΠυͰ͸ɺΈͳ͞Μ͕J04 ΞϓϦΛ࡞Δͷʹ࢖͏Α͏ͳ΋ͷΛʮ͍ΘΏΔ31ʯ·ͨ͸୯ʹ ʮ31ʯͱݴ͍·͢ɻ ͍ΘΏΔRP

    ϦΞΫςΟϒϓϩάϥϛϯά (ҰൠͷRPؚΉ) FRP ଞͷRP Functional Reactive Programming iOSΞϓϦΛ࡞Δͱ͖ʹ ࢖͏RP αʔόαΠυ Ϋϥ΢υ ͱ͔
  6. © ZOZO, Inc. 17 RPؔ࿈ਤ ......ྩ࿨4೥࠷৽൛(งғؾ) struct, class, var, didSet,

    closure, property wrapper, … Swiftجຊػೳ async/await, AsyncStream, … Swift Concurrency swift-async-algorithms, AsyncChannel, … ඇಉظϓϩάϥϛϯά ReactiveSwift RxSwift Combine + UIͷΞμϓλ RPϥΠϒϥϦ ·ͩͳ͍? RPϥΠϒϥϦ (iOSΞϓϦ޲͚) RP (Ұൠ) ͍ΘΏΔϦΞΫςΟϒϓϩάϥϛϯά RP (iOSΞϓϦ)
  7. © ZOZO, Inc. 18 RPؔ࿈ਤ ......Swift ConcurrencyҎલ struct, class, var,

    didSet, closure, property wrapper, … Swiftجຊػೳ ඇಉظϓϩάϥϛϯά ReactiveSwift, RxSwift, Combine + UIͷΞμϓλ RPϥΠϒϥϦ RP (iOSΞϓϦ) RPϥΠϒϥϦΛඇಉظ༻్Ͱ ࢖͍ͬͯΔέʔε ͨͩͷඇಉظ completion handlerͱ͔ ͍ΘΏΔϦΞΫςΟϒϓϩάϥϛϯά MVVMͷܨ͗ͱ͔ completionͱdidSetͰܨ͍Ͱ΋޿ٛͷRP͔΋...?
  8. © ZOZO, Inc. 19 RPؔ࿈ਤ ......Swift ConcurrencyҎ߱Ͳ͏ม͑Δ͔ struct, class, var,

    didSet, closure, property wrapper, … Swiftجຊػೳ async/await, AsyncStream, … Swift Concurrency ඇಉظϓϩάϥϛϯά ReactiveSwift, RxSwift, Combine + UIͷΞμϓλ RPϥΠϒϥϦ RP (iOSΞϓϦ) RPϥΠϒϥϦΛඇಉظ༻్Ͱ ࢖͍ͬͯΔέʔε ͨͩͷඇಉظ ͍ΘΏΔϦΞΫςΟϒϓϩάϥϛϯά MVVMͷܨ͗ͱ͔ async/awaitͱdidSetͰܨ͍Ͱ΋޿ٛͷRP͔΋...? Swift ConcurrencyΛ ࢖ͬͨ΄͏͕ ྑͦ͞͏ ͦͷ·· RPϥΠϒϥϦΛ ࢖͏ͱྑͦ͞͏
  9. © ZOZO, Inc. 20 struct, class, var, didSet, closure, property

    wrapper, … Swiftجຊػೳ async/await, AsyncStream, … Swift Concurrency ඇಉظϓϩάϥϛϯά ReactiveSwift, RxSwift, Combine + UIͷΞμϓλ RPϥΠϒϥϦ RP (iOSΞϓϦ) RPϥΠϒϥϦΛඇಉظ༻్Ͱ ࢖͍ͬͯΔέʔε ͨͩͷඇಉظ ͍ΘΏΔϦΞΫςΟϒϓϩάϥϛϯά MVVMͷܨ͗ͱ͔ async/awaitͱdidSetͰܨ͍Ͱ΋޿ٛͷRP͔΋...? Swift ConcurrencyΛ ࢖ͬͨ΄͏͕ ྑͦ͞͏ ͦͷ·· RPϥΠϒϥϦΛ ࢖͏ͱྑͦ͞͏ ͍ΘΏΔRPͷ෦෼΁
  10. © ZOZO, Inc. 22 σʔλϑϩʔ 31Ͱ͸6*ΛؚΉσʔλϑϩʔΛ੩తʹهड़ ͢Δͷ͕໨త ྫӈͷؔ܎Λ੩తʹهड़͍ͨ͠ 31ͷͳ͍4XJGUͰ͸ɺʮ͜ΕΛ࠶ܭࢉ͢ Δʯ໋ྩจ͕ඞཁ

    ੩తʹॻ͚ͳ͍ ͜ͷྫ: https://reactivecocoa.io/philosophy.html in in out A = B + C var a: Int var b: Int { didSet { a = b + c } } var c: Int { didSet { a = b + c } }
  11. © ZOZO, Inc. 23 ໋ྩతͱ͔એݴతͱ͔ એݴతͷྨٛޠ ੩త ͦΕͬΆ͍ྫ "VUP-BZPVU $PEBCMFͰ"1*

    ϦΞΫςΟϒϓϩάϥϛϯά ໋ྩతͷྨٛޠ ಈతɾखଓత ͦΕͬΆ͍ྫ खಈϨΠΞ΢τv.frame = ... ࣙॻऔΓग़͠Ͱ"1*d["id"] = 42 खͰσʔλߋ৽updateState(); updateUI()
  12. © ZOZO, Inc. એݴతͷྨٛޠ ੩త ͦΕͬΆ͍ྫ "VUP-BZPVU $PEBCMFͰ"1* ϦΞΫςΟϒϓϩάϥϛϯά ໋ྩతͷྨٛޠ

    ಈతɾखଓత ͦΕͬΆ͍ྫ खಈϨΠΞ΢τv.frame = ... ࣙॻऔΓग़͠Ͱ"1*d["id"] = 42 खͰσʔλߋ৽updateState(); updateUI() 24 ઐ໳ಛԽ ඼࣭Αͦ͞͏ ͦΕͧΕษڧ͠ͳ͍ͱग़དྷͳ͍ ͠͹͠͹ʮΉ͍ͣʯͱ͞ΕΔ ͍ͭͰ΋ྗٕͰॻ͚Δ ॲཧൈ͚Ͱෆ۩߹ग़ͦ͏ ଞͷݴޠ͔͠஌Βͳͯ͘΋ษڧ͠ͳ͘ ͯ΋ͳΜͱ͔ॻ͚ͦ͏ ʮ࢓༷ɾઃܭʯʹ͍ۙهड़ ʮ࢓༷ɾઃܭʯΛ໋ྩʹ຋༁͢Δ ͍͔ʹ΋ͳϓϩάϥϛϯάͷ͓͠͝ͱ ໋ྩతͱ͔એݴతͱ͔
  13. © ZOZO, Inc. એݴతͷྨٛޠ ੩త ͦΕͬΆ͍ྫ "VUP-BZPVU $PEBCMFͰ"1* ϦΞΫςΟϒϓϩάϥϛϯά ໋ྩతͷྨٛޠ

    ಈతɾखଓత ͦΕͬΆ͍ྫ खಈϨΠΞ΢τv.frame = ... ࣙॻऔΓग़͠Ͱ"1*d["id"] = 42 खͰσʔλߋ৽updateState(); updateUI() 25 ઐ໳ಛԽ ඼࣭Αͦ͞͏ ͦΕͧΕษڧ͠ͳ͍ͱग़དྷͳ͍ ͠͹͠͹ʮΉ͍ͣʯͱ͞ΕΔ ͍ͭͰ΋ྗٕͰॻ͚Δ ॲཧൈ͚Ͱෆ۩߹ग़ͦ͏ ଞͷݴޠ͔͠஌Βͳͯ͘΋ษڧ͠ͳ͘ ͯ΋ͳΜͱ͔ॻ͚ͦ͏ ʮ࢓༷ɾઃܭʯʹ͍ۙهड़ ʮ࢓༷ɾઃܭʯΛ໋ྩʹ຋༁͢Δ ͍͔ʹ΋ͳϓϩάϥϛϯάͷ͓͠͝ͱ ໋ྩతͱ͔એݴతͱ͔
  14. © ZOZO, Inc. એݴతͷྨٛޠ ੩త ͦΕͬΆ͍ྫ "VUP-BZPVU $PEBCMFͰ"1* ϦΞΫςΟϒϓϩάϥϛϯά ໋ྩతͷྨٛޠ

    ಈతɾखଓత ͦΕͬΆ͍ྫ खಈϨΠΞ΢τv.frame = ... ࣙॻऔΓग़͠Ͱ"1*d["id"] = 42 खͰσʔλߋ৽updateState(); updateUI() 26 ઐ໳ಛԽ ඼࣭Αͦ͞͏ ͦΕͧΕษڧ͠ͳ͍ͱग़དྷͳ͍ ͠͹͠͹ʮΉ͍ͣʯͱ͞ΕΔ ͍ͭͰ΋ྗٕͰॻ͚Δ ॲཧൈ͚Ͱෆ۩߹ग़ͦ͏ ଞͷݴޠ͔͠஌Βͳͯ͘΋ษڧ͠ͳ͘ ͯ΋ͳΜͱ͔ॻ͚ͦ͏ ʮ࢓༷ɾઃܭʯʹ͍ۙهड़ ʮ࢓༷ɾઃܭʯΛ໋ྩʹ຋༁͢Δ ͍͔ʹ΋ͳϓϩάϥϛϯάͷ͓͠͝ͱ ໋ྩతͱ͔એݴతͱ͔
  15. © ZOZO, Inc. એݴతͷྨٛޠ ੩త ͦΕͬΆ͍ྫ "VUP-BZPVU $PEBCMFͰ"1* ϦΞΫςΟϒϓϩάϥϛϯά ໋ྩతͷྨٛޠ

    ಈతɾखଓత ͦΕͬΆ͍ྫ खಈϨΠΞ΢τv.frame = ... ࣙॻऔΓग़͠Ͱ"1*d["id"] = 42 खͰσʔλߋ৽updateState(); updateUI() 27 ઐ໳ಛԽ ඼࣭Αͦ͞͏ ͦΕͧΕษڧ͠ͳ͍ͱग़དྷͳ͍ ͠͹͠͹ʮΉ͍ͣʯͱ͞ΕΔ ͍ͭͰ΋ྗٕͰॻ͚Δ ॲཧൈ͚Ͱෆ۩߹ग़ͦ͏ ଞͷݴޠ͔͠஌Βͳͯ͘΋ษڧ͠ͳ͘ ͯ΋ͳΜͱ͔ॻ͚ͦ͏ ʮ࢓༷ɾઃܭʯʹ͍ۙهड़ ʮ࢓༷ɾઃܭʯΛ໋ྩʹ຋༁͢Δ ͍͔ʹ΋ͳϓϩάϥϛϯάͷ͓͠͝ͱ ໋ྩతͱ͔એݴతͱ͔
  16. © ZOZO, Inc. 28 σʔλϑϩʔ A = B + C

    C B A + in in out ͪͳΈʹσʔλϑϩʔͷਤͱ͍͏ͱ ԼͷΑ͏ͳײ͡ ͜ͷྫ: https://reactivecocoa.io/philosophy.html
  17. © ZOZO, Inc. 29 σʔλͷശ + ઀ଓͷ໼ҹ C B A

    + ʮม਺ͱ͸ശͰ͋ΔʯശΛܨ͍ͩ΋ͷ͕ σʔλϑϩʔͰ͋Γ31 ͨͩ͠σʔλϑϩʔͷശ͸ɺ มߋ఻೻ͷػೳΛ΋ͭ͜ͱ MutableProperty BehaviorRelay CurrentValueSubject @Published ֤ϥΠϒϥϦͰͷʮശʯ
  18. © ZOZO, Inc. 30 σʔλͷശ + ઀ଓͷ໼ҹ C B A

    + map flatMap filter combineLatest : ശͱശͷ઀ଓ͸໼ҹ ্ྲྀɾԼྲྀ͕͋Δ  ʮΦϖϨʔλʔʯͱݺ͹ΕΔ ֤ϥΠϒϥϦͰͷʮ໼ҹʯ
  19. © ZOZO, Inc. ؅ཧ͢΂͖ঢ়ଶ͸ͲΕ͚ͩ͋Δ͔ ͦΕΛശʹ͢Δ ঢ়ଶ͕มΘͬͨͱ͖ɺശ͔Βശ΁఻೻͢΂͖͔ ͦΕΛ໼ҹʹ͢Δ σʔλϑϩʔͷ֎͔ΒมߋͰ͖Δശ͸  σʔλϑϩʔͷ֎΁Өڹ͢Δശ͸

     มߋ఻೻͕͋Δ͚ͩͰɺϝϯόม਺ͷઃܭͱมΘΒͳ͍ Ұ࣌ม਺΋Ұ࣌తͳശ΋ϝϯόʹ࣋ͨͳ͍͍ͯ͘ 32 σʔλϑϩʔΛઃܭ͢Δ
  20. © ZOZO, Inc. 34 UIͱͷ઀ଓ(ೖޱɾग़ޱ) *#"DUJPOͰड໋͚ͯྩతʹೖྗB.value = text 6*ΞμϓλͰശ૬౰ͷ΋ͷ͕͋ΔB =

    fieldB.reactive.textValues B A PCTFSWF TVCTDSJCF TJOL໋ͯ͠ྩతʹग़ྗlabelA.text = $0 6*ΞμϓλͰശ૬౰ͷ΋ͷ͕͋ΔlabelA.reactive.text <~ A σʔλϑϩʔͷ֎ͱ͸໋ྩతʹ΍ΓͱΓ͢Δ͔ɺϥΠϒϥϦʹ༻ҙ͞Ε ͍ͯΔ6*ΞμϓλΛ࢖ͬͯσʔλϑϩʔͷҰ෦ͱͯ͠઀ଓ͢Δ
  21. © ZOZO, Inc. APIϨεϙϯε ετϦʔϛϯά payload 35 UIҎ֎ͷೖޱɾग़ޱ 6*ʹ௚͍݁ͯ͠ͳ͍ΠϕϯτΛೖޱ΍ग़ޱͱ͢Δ͜ͱ͸͋Δ ηϯαʔ஋

    ݕࡧςΩετ ೖޱ ηϯαʔ J#FBDPOΛݕ஌ͨ͠ͱ͔ɺͳΜͰ΋ ωοτϫʔΫ ݕࡧςΩετ͕มߋ͞ΕͨΒ"1*ϦΫΤετ ग़ޱ
  22. © ZOZO, Inc. 38 ͍ΘΏΔඇಉظϓϩάϥϛϯάͷ෦෼͸ɺ ঃʑʹ4XJGU$PODVSSFODZʹ͍ͯ͘͠ͱγϯϓϧ Swift Concurrency͔? RP͔? ඇಉظϓϩάϥϛϯά

    RPϥΠϒϥϦΛඇಉظ༻్Ͱ ࢖͍ͬͯΔέʔε ͨͩͷඇಉظ Swift ConcurrencyΛ ࢖ͬͨ΄͏͕ ྑͦ͞͏ σʔλϑϩʔͰܨ͗͜ΊΔͱ͜Ζ͸ɺ31Λ࢖͏ 6*΍ωοτϫʔΫͱͷ઀ଓ͸ࠓޙ΋ඞཁ
  23. © ZOZO, Inc. 39 RPؔ࿈ਤ ......࠶ܝʂ͓͞Β͍͠Α͏ struct, class, var, didSet,

    closure, property wrapper, … Swiftجຊػೳ async/await, AsyncStream, … Swift Concurrency ඇಉظϓϩάϥϛϯά ReactiveSwift, RxSwift, Combine + UIͷΞμϓλ RPϥΠϒϥϦ RP (iOSΞϓϦ) RPϥΠϒϥϦΛඇಉظ༻్Ͱ ࢖͍ͬͯΔέʔε ͨͩͷඇಉظ ͍ΘΏΔϦΞΫςΟϒϓϩάϥϛϯά MVVMͷܨ͗ͱ͔ async/awaitͱdidSetͰܨ͍Ͱ΋޿ٛͷRP͔΋...? Swift ConcurrencyΛ ࢖ͬͨ΄͏͕ ྑͦ͞͏ ͦͷ·· RPϥΠϒϥϦΛ ࢖͏ͱྑͦ͞͏
  24. © ZOZO, Inc. 40 ͍ΘΏΔඇಉظϓϩάϥϛϯάͱݴ͍ͬͯΔ͕ ྫ͑͹͜͏͍͏ͱ͜Ζ "1*ίʔϧݸ Ұ࿈ͷBTZODGVODΛϩʔΧϧͰ·ͱΊ͍ͨ Swift Concurrency͔?

    RP͔? func f() async -> T { let a = await fa() let b = await fb(a) return b } func request() async -> Response