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

Updating an App to Use Swift Concurrency 解説

Updating an App to Use Swift Concurrency 解説

Apple のサンプルプロジェクト Updating an App to Use Swift Concurrency の全体像の把握とサンプルプロジェクトを作成した人の気持ちを考えてみる為の資料。

naru-jpn

May 12, 2022
Tweet

More Decks by naru-jpn

Other Decks in Programming

Transcript

  1. &OUJUZ struct Drink: Hashable, Codable { let mgCaffeine: Double let

    date: Date let uuid: UUID } enum DrinkType: Int, CaseIterable, Identifiable { case smallCoffee case mediumCoffee case largeCoffee case singleEspresso case doubleEspresso case quadEspresso case blackTea case greenTea case softDrink case energyDrink case chocolate } %SJOL %SJOL5ZQF
  2. $PGGFF%BUB class CoffeeData: ObservableObject { lazy var healthKitController = HealthKitController(withModel:

    self) // @Published ͳͷͰϝΠϯεϨου͔Βૢ࡞͢Δඞཁ͕͋Δ @Published public var currentDrinks: [Drink] = [] // … func save() { /* currentDrinks ΛσΟεΫ΁อଘ(όοΫάϥ΢ϯυॲཧ) */ } func load() { /* currentDrinks ΛσΟεΫ͔ΒಡΈࠐΈ(όοΫάϥ΢ϯυॲཧ) */ } }
  3. )FBMUI,JU$POUSPMMFS // … runs the query on an anonymous background

    queue. private let store = HKHealthStore() class HealthKitController { private weak var model: CoffeeData? // … public func loadNewDataFromHealthKit(…) { /* … */ } public func save(drink: Drink) { /* … */ } private func updateModel(newDrinks: [Drink], deletedDrinks: Set<UUID>) { // … // @Published ͳͷͰϝΠϯεϨου͔Βૢ࡞͢Δඞཁ͕͋Δ model?.currentDrinks = drinks } }
  4. $PGGFF5SBDLFS7JFX $PGGFF%BUB )FBMUI,JU$POUSPMMFS %SJOL-JTU7JFX .BJO5ISFBE #BDLHSPVOE5ISFBE DVSSFOU%SJOLT ),)FBMUI4UPSF TUPSFFYFDVUF RVFSZ

    SFTVMUT)BOEMFS %FWJDF 3FBE8SJUF%BUB BEE%SJOL ͜͜Λ੔ཧ͍ͨ͠ NPEFMDVSSFOU%SJOLTESJOLT
  5. $PGGFF5SBDLFS7JFX $PGGFF%BUB )FBMUI,JU$POUSPMMFS %SJOL-JTU7JFX .BJO5ISFBE #BDLHSPVOE5ISFBE DVSSFOU%SJOLT ),)FBMUI4UPSF TUPSFFYFDVUF RVFSZ

    SFTVMUT)BOEMFS %FWJDF 3FBE8SJUF%BUB BEE%SJOL ௚઀ଞͷΦϒδΣΫτͷϓϩύςΟΛ ߋ৽͢ΔͷͰ͸ͳ͘ɺBDUPSؒͷ ΍ΓͱΓʹஔ͖׵͑Δඞཁ͕͋Δ NPEFMDVSSFOU%SJOLTESJOLT
  6. $PGGFF%BUB @MainActor class CoffeeData: ObservableObject { lazy var healthKitController =

    HealthKitController(withModel: self) private let store = CoffeeDataStore() @Published public private(set) var currentDrinks: [Drink] = [] // … } currentDrinks ͸ private(set) ʹͳΓ CoffeeData ಺ͰͷΈߋ৽͞ΕΔΑ͏ʹ ৽ͨʹఆٛͨ͠ CoffeeDataStore ͸ϓϩύςΟͱͯ͠อ͍࣋ͯ͠Δ HealthKitController ͱͷؔ܎ੑ͸มΘΒͣ
  7. $PGGFF%BUB4UPSF actor CoffeeDataStore { func save(_ currentDrinks: [Drink]) { /*

    currentDrinks ΛσΟεΫ΁อଘ */ } func load() -> [Drink] { /* currentDrinks ΛσΟεΫ͔ΒಡΈࠐΈ */ } // … } ෼཭લͷؔ਺͸ͦΕͧΕ save(), load() save ʹ͸Ҿ਺͕ɺload ʹ͸ฦΓ஋͕෇͍ͨ ΞΫλʔؒͰඞཁͳ৘ใΛ΍ΓͱΓ͢ΔͨΊͷΠϯλʔϑΣʔε
  8. )FBMUI,JU$POUSPMMFS actor HealthKitController { private weak var model: CoffeeData? func

    loadNewDataFromHealthKit() async -> Bool { // … let (samples, deletedSamples, newAnchor) = try await queryHealthKit() // … await model?.updateModel(newDrinks: newDrinks, deletedDrinks: deletedDrinks) // … } // … } CoffeeData ͱͷؔ܎ੑ͸มΘΒͣ ௚઀ currentDrink Λߋ৽͢ΔͷͰ͸ͳ͘ɺupdateModel Λհͯ͠ߋ৽͢Δ ΫΤϦͷ࣮ߦ෦෼ͷهड़͕ΦγϟϨʹ