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

SwiftData: Dive into inheritance and schema mig...

Avatar for 1mash0 1mash0
June 26, 2025
210

SwiftData: Dive into inheritance and schema migration - Swift愛好会スピンオフ WWDC25セッション要約会

Avatar for 1mash0

1mash0

June 26, 2025
Tweet

Transcript

  1. Harness class inheritance iOS 26͔ΒSwiftDataϞσϧͷΫϥεܧঝ͕αϙʔτ͞Εͨ ܧঝΛ༻͍Δ͜ͱͰɺαϒΫϥε͸਌ΫϥεͷϓϩύςΟ΍ৼΔ෣͍ΛҾ͖ܧ͙͜ͱ ͕Ͱ͖Δ @Model class Trip

    { ... } @available(iOS 26, *) @Model class BusinessTrip: Trip { var perdiem: Double = 0.0 } @available(iOS 26, *) @Model class PersonalTrip: Trip { var reason: Reason }
  2. Harness class inheritance ࢖͍ํ͸ෳ਺ͷϞσϧΛѻ͍͍ͨ࣌ͱಉ͡Α͏ʹ౉͚ͩ͢ WindowGroup { ContentView() } .modelContainer(for: [Trip.self,

    PersonalTrip.self, BusinessTrip.self]) let modelContainer = try ModelContainer( for: Trip.self, PersonalTrip.self, BusinessTrip.self ) WindowGroup { ContentView() } .modelContainer(modelContainer)
  3. Evolving data with migration VersionedSchemaͱSchemaMigrationPlanΛ༻͍ͯɺաڈόʔδϣϯ͔Βͷ ϚΠάϨʔγϣϯ͕Մೳ iOS 17: εΩʔϚόʔδϣϯ2.0 SwiftData͕ಋೖ

    iOS 18: εΩʔϚόʔδϣϯ3.0 ॏෳσʔλΛղফ͢ΔͨΊʹΧελϜͷMigrationStageΛ࢖༻ iOS 26: εΩʔϚόʔδϣϯ4.0 αϒΫϥεΛ௥Ճ ηογϣϯதͩͱMigrationStage.lightweightͰϚΠάϨʔγϣϯ͕ߦΘ ΕͯΔ
  4. Observing changes to data iOS 26͔ΒཤྺΛsortByͰฒ΂ସ͑ͯऔಘͰ͖ΔΑ͏ʹͳͬͨ ࠓ·Ͱ͸ཤྺΛશ݅औಘͯ͠͠·͏Մೳੑ͕͕͋ͬͨɺޮ཰తʹ࠷৽ͷཤྺτʔΫϯΛ औಘͰ͖ΔΑ͏ʹͳͬͨ var historyDesc

    = HistoryDescriptor<DefaultHistoryTransaction>() historyDesc.sortBy = [.init(\.transactionIdentifier, order: .reverse)] historyDesc.fetchLimit = 1 let transactions = try context.fetchHistory(historyDesc)