$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
SwiftData: Dive into inheritance and schema mig...
Search
1mash0
June 26, 2025
0
380
SwiftData: Dive into inheritance and schema migration - Swift愛好会スピンオフ WWDC25セッション要約会
1mash0
June 26, 2025
Tweet
Share
More Decks by 1mash0
See All by 1mash0
SwiftDataのカスタムデータストアを試してみた
1mash0
0
250
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
55
9.4k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
0
390
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.1k
Visualization
eitanlees
150
16k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
75
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
140
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
250
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
0
59
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
61
47k
A Tale of Four Properties
chriscoyier
162
23k
Transcript
ू·ΕSwift͖ʂSwiftѪձεϐϯΦϑ WWDC25ηογϣϯཁձ SwiftData: Dive into inheritance and schema migration ͍·͑͢,
X: 1mash0_
Topics Harness class inheritance Evolving data with migration Tailoring fetched
data Observing changes to data
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 }
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)
Harness class inheritance ϙΠϯτ ΫϥεͱαϒΫϥε͕֊ߏΛ࣋ͭ͜ͱ is-a ͷ͕ؔΓཱͭ͜ͱ ֊ߏΛ࣋ͨͣɺ୯ҰϓϩύςΟͷڞ༗͕తͷ߹ϓϩτίϧ४ڌʹ͢Δ σΟʔϓαʔνͱγϟϩʔαʔνͷ྆ํΛར༻͢Δ͜ͱ σΟʔϓαʔνͷΈͷ߹ɺΫϥεͷϓϩύςΟͱݟͳ͖͢
γϟϩʔαʔνͷΈͷ߹ɺͦΕͧΕͰϞσϧΛ࡞͢Δ͖
Evolving data with migration VersionedSchemaͱSchemaMigrationPlanΛ༻͍ͯɺաڈόʔδϣϯ͔Βͷ ϚΠάϨʔγϣϯ͕Մೳ iOS 17: εΩʔϚόʔδϣϯ2.0 SwiftData͕ಋೖ
iOS 18: εΩʔϚόʔδϣϯ3.0 ॏෳσʔλΛղফ͢ΔͨΊʹΧελϜͷMigrationStageΛ༻ iOS 26: εΩʔϚόʔδϣϯ4.0 αϒΫϥεΛՃ ηογϣϯதͩͱMigrationStage.lightweightͰϚΠάϨʔγϣϯ͕ߦΘ ΕͯΔ
Tailoring fetched data SampleTripsΞϓϦʹݕࡧόʔͰͷݕࡧػೳ͕࠶ಋೖ ηάϝϯτͱݕࡧςΩετͷPredicateΛΈ߹ΘͤͯϑΟϧλϦϯά͍ͯ͠Δ αϒΫϥεʹߜͬͯϑΟϧλϦϯά͢ΔPredicateͷੜ let predicate: Predicate<Trip>? =
#Predicate { $0 is PersonalTrip }
Tailoring fetched data ϚΠάϨʔγϣϯ࣌ͷϑΣονॲཧύϑΥʔϚϯε্ͷ propertiesToFetchΛ༻ͯ͠ඞཁͳϓϩύςΟͷσʔλ͚ͩΛ࣋ͬͨϞσϧΛ औಘ͢ΔΑ͏࠷దԽ ಛఆͷϦϨʔγϣϯγοϓΛḷΔඞཁ͕͋Δ߹ relationshipKeyPathsForPrefetchingΛͬͯ࠷దԽΛਤΕΔ SampleTripsΞϓϦͷΟδΣοτͰ࠷৽ͷ1݅ͷΈ͕ඞཁͳͨΊɺfetchLimit Λઃఆͯ͠ޮΛߴΊ͍ͯΔ
Observing changes to data PersistentModelObservable withObservationTrackingΛ͏͜ͱͰϞσϧͷϓϩύςΟʹՃ͑ΒΕͨมߋΛ ࢹͰ͖Δ ͨͩ͠؍ଌͰ͖ΔͷಉҰͷϓϩηεͰߦΘΕͨมߋͷΈ ΟδΣοτɺΤΫεςϯγϣϯɺΞϓϦͷผModelContainer͔ΒՃ͑ΒΕͨ มߋ؍ଌͰ͖ͳ͍
Observing changes to data ಉҰModelContainerͰɺผModelContextͰ͋ͬͯQueryΛ༻͍ͯ͠Δ ߹มߋ͕ࣗಈͰө͞ΕΔ ϑΣονAPIͳͲΛར༻͍ͯ͠Δ߹ɺผModelContextͰՃ͑ΒΕͨมߋ࠶ϑΣο ν͠ͳ͍ͱө͞Εͳ͍ ࠶ϑΣονίετ͕ߴ͍ͨΊɺཤྺػೳΛར༻ͯ͠࠶ϑΣον͕ඞཁ͔Ͳ͏͔Λஅ ͢ΔΑ͏ʹ͢Δ
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)