Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
SwiftData: Dive into inheritance and schema mig...
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
1mash0
June 26, 2025
0
420
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
280
Featured
See All Featured
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
490
Why Our Code Smells
bkeepers
PRO
340
58k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
390
Git: the NoSQL Database
bkeepers
PRO
432
67k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
A Tale of Four Properties
chriscoyier
163
24k
Thoughts on Productivity
jonyablonski
75
5.1k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
500
The Mindset for Success: Future Career Progression
greggifford
PRO
0
290
How STYLIGHT went responsive
nonsquared
100
6k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.6k
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)