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
430
0
Share
SwiftData: Dive into inheritance and schema migration - Swift愛好会スピンオフ WWDC25セッション要約会
1mash0
June 26, 2025
More Decks by 1mash0
See All by 1mash0
SwiftDataのカスタムデータストアを試してみた
1mash0
0
280
Featured
See All Featured
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Code Review Best Practice
trishagee
74
20k
Exploring anti-patterns in Rails
aemeredith
3
310
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
260
How to Talk to Developers About Accessibility
jct
2
170
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
350
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
250
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
270
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
110k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1k
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)