SwiftData

RSS for tag

Learn to write model code declaratively to add managed persistence and efficient model fetching.

SwiftData Documentation

Posts under SwiftData subtopic

Post

Replies

Boosts

Views

Activity

CoreData lightweight migration fails on iOS 26 only — "no such column: Z_110GROUPITEMS1"
We've spent several days diagnosing a CoreData migration crash that is iOS 26-specific and reproducible 100% of the time. Posting here in case others have hit this and because we believe it's an Apple bug worth documenting. Upgrading from our App Store build (CoreData model v10) to our latest TestFlight build (model v11) crashes on iOS 26 with: NSCocoaErrorDomain / Code 134110 no such column: "Z_110GROUPITEMS1" The same upgrade path on iOS 17 and iOS 18 works perfectly. What v10→v11 changes Two new entities added alphabetically early in the alphabet One new optional boolean attribute on an existing entity One new optional to-many relationship on the same existing entity All changes are lightweight-migration compatible. We use shouldMigrateStoreAutomatically = true and shouldInferMappingModelAutomatically = true Here's what we observed Adding two entities alphabetically shifts Z_ENT numbers for all subsequent entities. A central entity (EntityA) moves from Z_ENT 110 (v10) to Z_ENT 112 (v11). It has many-to-many relationships with four other entities (EntityB, EntityC, EntityD, EntityE), all using the same inverse relationship name: groupItems. Because multiple join tables reference EntityA via the same inverse name, CoreData appends a disambiguation suffix (1, 2, etc.) to column names in each join table. In v10, the relevant join tables are Z_110ENTITYB and Z_110ENTITYC, each with a column named Z_110GROUPITEMS + a suffix. -com.apple.CoreData.SQLDebug 3 prints: ALTER TABLE Z_112ENTITYB RENAME COLUMN Z_110GROUPITEMS1 TO Z_112GROUPITEMS1 But the actual column in a fresh iOS 26 v10 store is Z_110GROUPITEMS2. Column not found → crash. iOS 17/18 is consistent: both code paths use suffix 1 for Z_110ENTITYB and 2 for Z_110ENTITYC. Migration succeeds. To confirm We opened the SQLite store from a fresh iOS 26 v10 install and inspected the schema: CREATE TABLE Z_110ENTITYB ( Z_110GROUPITEMS2 INTEGER, Z_112ENTITYB INTEGER, PRIMARY KEY (Z_110GROUPITEMS2, Z_112ENTITYB) ); Then we manually renamed Z_110GROUPITEMS2 → Z_110GROUPITEMS1 and Z_110ENTITYC.Z_110GROUPITEMS1 → Z_110GROUPITEMS2 in the SQLite file. Re-ran the app — migration succeeded. The suffixes are literally swapped between what iOS 26 creates on fresh install vs. what iOS 26's migration engine expects. Our database has over 50 entities and we never before faced such an issue. This is not the first lightweight migration we are releasing after iOS 26 and that's what puzzled us. Why now?
2
0
72
12h
Fetch data in a time range, plus extend one more entry
I’m using Swift Charts in my app to display data in a visual way. For that (as well as some sum calculations) I’d like to create a fetch request that searches for all the data in a given time range plus one older and newer entry. Building a predicate that searches for a time range is easy, but how can I tell the fetch request that I would also like to have the first entry that comes before that time range and the first that comes after? I don’t know when these entries appear, so I can’t extend the time range. Example Let’s say I have the following data (timestamp – value): Feb 1, 2024 – 20 Oct 1, 2024 – 30 Jan 1, 2025 – 40 Sep 1, 2025 – 50 May 1, 2026 – 60 Oct 1, 2026 – 70 Now I want to search for the range Jan to Dec 2025. How can I retrieve the two entries that are in the range (No. 3 & 4) as well as the first entry before (No. 2) and the first after that (No. 5)? Without knowing the timestamp of No. 2 and 5. I’d appreciate some best practices or tips how to handle this special case. 🙏
1
0
58
12h
SwiftData predicate filtered by enum case
I have several Swift Data types with a property of type enum. Whenever I've tried to write a predicate returning data objects only of a certain enum case, the compiler throws an error from the macro at build time. (which I don't have handy, sorry...). Is this supported? And if so, how would you write the predicate? @Model public final class AlbumList { // ... public var listType: AlbumListType // ... } public enum AlbumListType: String, CaseIterable, Codable { case listener case dj }
2
0
52
12h
Migration Hash Mismatch
I'm migrating an existing Core Data app to SwiftData and encountering a persistent schema hash mismatch that prevents the store from loading. The Core Data store has been in production for years with version 4 of the model. I have two models, lets call the Crew and Astronaut Create is a parent in the to-many relationship to Astronaut, the child. When I try the migration, the Crew model‘s hash matches, but the Astronaut model’s mismatches. I also tried changing relationship parameters; deleteRule, maximumModelCount, inverse, originalName, and hashModifier. hashModifier changes the hash, but to a third value, so it does not appear to be a way to match the existing Core Data hash. So, is there a supported way for SwiftData to generate an optional to-one relationship that is Core Data hash-compatible with optional=YES minCount=1 maxCount=1? Or is a migration/rehash/copy step required before SwiftData can adopt this V4 store in place?
3
0
46
12h
SwiftData + public CloudKitDatabase?
Greetings, Thank you for SwiftData + new features. I am looking forward to the SwiftData Group Lab on Friday. How does the performance of SwiftData + CloudKit private database compare to CoreData + NSPersistenceCloudKitContainer? Also, will SwiftData + CloudKit support the CK public database, in the future? Thank you!
2
2
64
12h
Dynamic Compound Predicates
This is relating to the question I have for the App Intents framework (see my question). I know that SwiftData started to support compound predicates with macOS 14.4/iOS 17.4 or later. But from what I understand they are not dynamic and are validated at compile time. Is there a way to update/construct predicates while the app is running? For example to create a search tab that allows searching and filtering for my items in the app.
1
0
49
13h
889 CoreData errors in a SwiftData app of less than 30 lines
I have created a SwiftData iOS app from Paul Hudson's Hacking With Swift series in order to troubleshoot some SwiftData issues I am having in a separate app. I have raised > FB22925785 I have gone back to basics to try and problem solve so I have used the first part of an app of Paul's from [https://www.hackingwithswift.com/quick-start/swiftdata/defining-a-data-model-with-swiftdata] The App is very simple and only contains the following import SwiftData import SwiftUI @main struct iTour_from_scratchApp: App { var body: some Scene { WindowGroup { ContentView() .modelContainer(for: Destination.self) } } } The data is import SwiftData @Model class Destination { var name: String var details: String var date: Date var priority: Int init(name: String, details: String, date: Date, priority: Int) { self.name = name self.details = details self.date = date self.priority = priority } } The view is import SwiftUI struct ContentView: View { var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("Hello, world!") } .padding() } } #Preview { ContentView() } When I try to run this, I am getting 889 lines of CoreData errors appearing in the Console and they start with CoreData: error: Failed to stat path '/Users/chrissantavy/Library/Developer/CoreSimulator/Devices/913BFD87-5FD8-47B9-AD0C-81238E74E89E/data/Containers/Data/Application/29586FDF-F08D-48E8-AF58-FD7BD5A30525/Library/Application Support/default.store', errno 2 / No such file or directory. CoreData: error: Executing as effective user 501 CoreData: error: Executing as effective user 501 CoreData: error: Sandbox access to file-write-create denied CoreData: error: Sandbox access to file-write-create denied CoreData: error: Failed to statfs file; errno 2 / No such file or directory. CoreData: error: Failed to statfs file; errno 2 / No such file or directory. CoreData: error: Information for file system CoreData: error: Information for file system CoreData: error: --------------------------- CoreData: error: --------------------------- CoreData: error: File system type: 0 CoreData: error: File system type: 0 CoreData: error: File system flags: 0 CoreData: error: File system flags: 0 CoreData: error: Total data blocks: 0 CoreData: error: Total data blocks: 0 CoreData: error: Free data blocks: 0 CoreData: error: Free data blocks: 0 CoreData: error: Free blocks for nonsuperuser: 0 CoreData: error: Free blocks for nonsuperuser: 0 CoreData: error: Total i-nodes: 0 CoreData: error: Total i-nodes: 0 CoreData: error: File system ID: 0, 0 CoreData: error: File system ID: 0, 0 CoreData: error: Free i-nodes: 0 CoreData: error: Free i-nodes: 0 CoreData: error: Owner UID: 0 CoreData: error: Owner UID: 0 CoreData: error: Filesystem type name: CoreData: error: Filesystem type name: CoreData: error: Mount on name: CoreData: error: Mount on name: CoreData: error: Mount from name: CoreData: error: Mount from name: CoreData: error: Failed to stat path '/Users/chrissantavy/Library/Developer/CoreSimulator/Devices/913BFD87-5FD8-47B9-AD0C-81238E74E89E/data/Containers/Data/Application/29586FDF-F08D-48E8-AF58-FD7BD5A30525/Library/Application Support', errno 2 / No such file or directory. CoreData: error: Failed to stat path '/Users/chrissantavy/Library/Developer/CoreSimulator/Devices/913BFD87-5FD8-47B9-AD0C-81238E74E89E/data/Containers/Data/Application/29586FDF-F08D-48E8-AF58-FD7BD5A30525/Library/Application Support', errno 2 / No such file or directory. CoreData: error: Executing as effective user 501 The full list of errors is attached iTour from scratch Part-1 errors.txt
1
0
69
13h
SwiftData: class inheritance without entity inheritance
In SwiftData, how can I have class inheritance without entity inheritance? My understanding is when models are subclassed they then share the same SQLite table allowing one Query to return multiple model types. However what if we don't require that functionality and only wish to share some overridable logic. I.e. share a base class implementation with multiple @Model subclasses but each model is in it's own SQLite table. E.g. say I want a savedAt timestamp field in every model and have the logic for setting it across all models. Or do some validation that calls super to check parent implementation is valid too. Maybe the validation behaviours can be done using protocols and extensions some how? Thanks!
1
0
48
13h
Sectioned fetching: beyond String keyPaths?
Hello, world! First and foremost, thank you for all the enhancements made to SwiftData and kudos to the team! In Thomas’ video demonstrating “What’s new in SwiftData”, I notice the keyPath (trip.destination) used to create sections in the list is a String. I can’t help but wonder if a keyPath of another data type could be used (like trip.startDate), were one to want to create sections based on whether a trip is past or upcoming for example? Thank you in advance for your much appreciated input! Zoé
1
0
66
13h
SwiftData @Query with a Custom (non-standard) Sort Criterion
I have a @Query which I would like to sort using a custom criterion. I would like to use an array of SortDescriptors where I can specify a key path and a CUSTOM SortComparator. (I.e., I can write SortComparators which do what I want, but I can't use them in a SortDescriptor because there is no initializer which takes a SortCompartor which is not a String.StandardComparator.) This is not a question about dynamic sorting, which has well-known solutions. I am trying to perform a sort which cannot be satisfied by any existing SortDescriptor (or array of SortDescriptors). Any thoughts about how to resolve this situation?
2
0
73
5d
SwiftData 'simple' migration failing
This is a long post, so let me start with a summary: I am attempting to implement what "ought to be" a simple SwiftData migration, and am receiving the following fatal error from the ModelContainer initializer: NSCocoaErrorDomain Code=134504 "Cannot use staged migration with an unknown model version." The crash occurs both in the Simulator and on a physical device. Both the original schema and the new schema load and run as expected if loaded from scratch — so I conclude the Models are OK; it is the migration from the original schema to the new schema which is the issue. I have reported this as FB22652791 and Technical Incident Case # 19893980. I have two model projects available — one contrived, the other using my actual SwiftData models. Now the Details I am developing a SwiftUI/SwiftData app. I am (currently) using Xcode 26.5-beta-3. I set up an alpha-test build using the following approach: public class DatabaseSchema { public let dbSchema: Schema = Schema([ Model1.self, ... , Model16.self ], version: Schema.Version(0, 9, 0)) public var modelContainer: ModelContainer { let container: ModelContainer let modelConfiguration = ModelConfiguration(schema: dbSchema, isStoredInMemoryOnly: false) do { container = try ModelContainer(for: dbSchema, migrationPlan: nil, configurations: [modelConfiguration]) } catch { fatalError("Failed to creae model conainer") } return container } This defines database version 0.9. For version 1.0, I made three changes to the database: added an attribute of type String to Model2. added three attributes of type [Struct], where Struct conforms to Codable, Equatable and Hashable to Model3, and added a new model (which I'll call Model17) I define two schemas: public enum Schema090: VersionedSchema { public static var versionIdentifier = Schema.Version(0, 9, 0) public static var models: [any PersistentModel.Type] = [ Model1.self, Schema090.Model2.self, Schema090.Model3.self, ... ] } and public enum Schema100: VersionedSchema { public static var versionIdentifier = Schema.Version(1, 0, 0) public static var models: [any PersistentModel.Type] = [ Model1.swift, Schema100.Model2.self, Schema100.Model3.self, ..., Model16.self, Schema100.Model17.self ] } For models that changed, I use the following approach: public typealias Model3 = Schema100.Model3 extension Schema090 { @Model final class Model3 { ... } public init() { ... } } extension Schema100 { @Model final class Model3 { ... <added attributes, initialized> } public init() { ... } } The DatabaseSchema class was modified as follows: public class DatabaseSchema { public let dbSchema: Schema = Schema([ Model1.self, Schema100.Model2.self, Schema100.Model3.self, ... , Model16.self, Schema100.Model17.self ], version: Schema.Version(1, 0, 0)) public var modelContainer: ModelContainer { let container: ModelContainer let modelConfiguration = ModelConfiguration(schema: dbSchema, isStoredInMemoryOnly: false) do { container = try ModelContainer(for: dbSchema, migrationPlan: MigrationPlan.self, configurations: [modelConfiguration]) } catch { fatalError("Failed to creae model conainer") } return container } where the migration plan is the trivial custom migration that makes sure that all added attributes of existing records are properly initialized. enum MigrationPlan: SchemaMigrationPlan { static var schemas: [any VersionedSchema.Type] = [ Schema090.self, Schema100.self ] static var stages: [MigrationStage] = [version090ToVersion100] static let version090ToVersion100 = MigrationStage(fromVersion: Schema090.self, toVersion: Schema100.self, willMigrate: { _ in }, didMigrate: { context in let models = try context.fetch( FetchDescriptor<Schema100.Model3>()) for model in models { < initial the added attributes > { try context.save() }) } This is all simple stuff. Nothing particularly fancy here. But running this code always crashes in the ModelContainer initializer. In my two sample projects, I get two different error messages — in the contrived example, the error message is Code=134110 "An error occurred during persistent store migration." reason=Cannot migrate store in-place: Validation error missing attribute values on mandatory destination attribute, ... and in the sample project that uses my actual data model, I get NSCocoaErrorDomain Code=134504 "Cannot use staged migration with an unknown model version." My Thoughts Since obviously most folks are doing SwiftData migrations without the problems I am experiencing, the obvious possibilities are I'm doing something stupid that I just don't see. There is a problem because the original schema was given a version value of Schema.Version(0, 9, 0). (i.e., major version number was 0) There is a problem because I am adding an attribute of type [Struct] where Struct is Codable, Hashable, and Equatable. I.e., migration isn't working properly with attributes which are stored as their codable representations. Or maybe something else? In any case, any help you can offer would be greatly appreciated.
7
0
651
1w
TestFlight build crashes from fetch descriptor
I have a FetchDescriptor that uses starts(with:) which works fine in debug builds but crashes in TestFlight and archive. For background information I'm using iCloud and model inheritance where the property being used in fetch descriptor is defined on the superclass, the fetch descriptor is for the subclass. Implementation: static func fetchDescriptor(nameStartingWith prefix: String) -> FetchDescriptor<ColorAsset> { let predicate = #Predicate<ColorAsset> { asset in asset.name.starts(with: prefix) } return FetchDescriptor<ColorAsset>(predicate: predicate) } @Model public class Asset: Identifiable { // MARK: - Properties var name: String = "" .... } @available(macOS 26.0, *) @Model public class ColorAsset: Asset { ... }
4
1
415
1w
SwiftData crash on new property (Could not cast...)
I have a small example where adding a new property to a persisted Codable struct causes a crash on launch instead of decoding the missing property using its default value. Steps Run this app once and press "Insert Event" to persist data: import SwiftUI import SwiftData @main struct SwiftDataCrash: App { var body: some Scene { WindowGroup { ContentView() } .modelContainer(for: Event.self) } } struct ContentView: View { @Environment(\.modelContext) private var modelContext @Query private var events: [Event] var body: some View { VStack(spacing: 12) { Text("Events: \(events.count)") Button("Insert Event") { let event = Event( recurrence: Recurrence( interval: 1 ) ) modelContext.insert(event) try? modelContext.save() } List(events) { event in Text(String(describing: event.recurrence)) } } .padding() } } @Model final class Event { var recurrence: Recurrence? = nil init(recurrence: Recurrence? = nil) { self.recurrence = recurrence } } struct Recurrence: Codable { var interval: Int // STEP 2: // After first run + inserting an Event, uncomment this and run again. // Expected: old data decodes with default [] // Actual: SwiftData may crash while reading Event.recurrence // // var exceptionDates: [Date] = [] } Then uncomment: var exceptionDates: [Date] = [] and run again without deleting the store. Actual result App crashes on launch with: Could not cast value of type 'Swift.Optional<Any>' to 'Swift.Array<Foundation.Date>' The crash appears to happen inside generated SwiftData persisted-property getter code. Expected result I expected the old persisted Recurrence values to decode with: exceptionDates == [] Is this expected behavior or a SwiftData bug?
2
0
258
1w
Crash when adding large JSON data to SwiftData: Could not cast value of type '__NSCFBoolean' to 'NSString'
Within the app main struct, the SwiftData modelContainer is created by fetching and decoding JSON from remote site. The decoded data is inserted into the modelContext. Immediately thereafter, the app crashes with the error Could not cast value of type '__NSCFBoolean' to 'NSString' . I have run the JSON from the site through JSON lint and it is correctly structured. The JSON data is structured as follows: Overview object which includes a Result object which includes a List Array. Each List object includes a Books Array. I have run the app through AI and the result indicates that there is data corruption when adding the data to the database. The AI rewrote the code such that the book data is encoded into a data blob and then adds the data to the database, instead of adding the individual books (which is extensive.) This approach works without crashing, but I would like to find out if this is indeed a SwiftData issue.
4
0
168
Jul ’25
SwiftData App Crashes only in TestFlight...
I have been building a SwiftUI/SwiftData app for some time that has been working fine in both the simulator and devices I have loaded from Xcode. These include two iPhones an iPad and a VisionPro. I have gotten to the point where I wanted to do some external beta testing so uploaded the App to the AppStore and then had a couple of users download via TestFlight. Unfortunately the App crashes and I have been unable to figure out why based on the feedback that I have gotten. The app has 4 Models that are linked in a fairly straightforward manner. There is a Habit class that is the central object. An Aspiration class that may have several Habits associated with it. An Anchor Class and a Celebration Class both attach to a Habit. I was able to get my neighbor to use TestFlight to download the App and it crashed when trying to insert a Habit but Aspirations, Anchors and Celebrations work fine and can be added, stored and deleted without issue. I then had my neighbor connect their phone to my computer and I download the app directly and it works fine as expected even when not connected to the debugger so clearly something is different between the two environments and I am at a loss so could use someone with good knowledge to help me figure this out.
4
0
189
Jul ’25
How to handle completely adopting SwiftData when you need all the lightweight migrations defined in the xcdatamodeld?
At the end over every "how to adopt SwiftData in a CoreData app" it says, now go ahead and delete the xcdatamodeld files. But my xcdatamodeld contains all the migrations necessary to even support SwiftData in the first place. If I have 10 versions of my CoreData model, and the 10th matches the first version of my SwiftData model... But a user is on the 7th version, and then upgrade to the new app that doesn't contain any xcdatamodeld files.. How are you actually supposed to handle this? I don't want to keep supporting both. I just want to use SwiftData.
0
0
187
Jun ’25
SwiftData superclass prevents usage of ID
New subclassing is a great addition to SwiftData, while trying to utilize the superclass type for selection state I’m seeing the following error: @available(macOS 26.0, *) @Model public class Asset { … } var assetSelection: [Asset.ID] = [] Error: 'ID' is inaccessible due to '@_spi' protection level Replacing the type with a subclassed swift data model of Asset works, but to handle mixed selection and the new .dragContainer modifier I need to be able to use the superclass. Is this intended behavior?
7
0
247
Jun ’25
Add App Group to Existing SwiftData App
I have an existing app that uses SwiftData and now want to add widgets. I added the widget extension, created an App Group to use for the main app target and widget targets and successfully created the widget. However, when testing the updates I often experience data loss - as though the including the widget extension is creating a new instance of modelContainer. Am I missing something to ensure there won't be any data loss when adding the App Group and widget extension? For additional context: I’ve followed the Backyard Birds example code except that it uses a separate app package. My app does not use an external app package, but I am using some elements of the DataGeneration file. My files containing the SwiftData models have Target Memberships for both the main app target and widget extension target. In the TimelineProvider for my widgets, I'm doing the following: let modelContext = ModelContext(DataGeneration.container) init() { DataGeneration.generateAllData(modelContext: modelContext) } My DataGeneration file (simplified) is as follows. When adding the widget target, I sometimes see the log for "Creating instance of DataGeneration". import Foundation import SwiftData @Model class DataGeneration { var requiresInitialization: Bool = true init(requiresInitialization: Bool = true) { self.requiresInitialization = requiresInitialization } private func generateInitialData(modelContext: ModelContext) { if requiresInitialization { let budget = Budget() modelContext.insert(budget) requiresInitialization = false } } private static func instance(with modelContext: ModelContext) -> DataGeneration { if let result = try! modelContext.fetch(FetchDescriptor<DataGeneration>()).first { logger.info("Found instance of DataGeneration") return result } else { logger.info("Creating instance of DataGeneration") let instance = DataGeneration() modelContext.insert(instance) return instance } } static func generateAllData(modelContext: ModelContext) { let instance = instance(with: modelContext) instance.generateInitialData(modelContext: modelContext) } } extension DataGeneration { static let container = try! ModelContainer(for: schema, configurations: [.init(isStoredInMemoryOnly: DataGenerationOptions.inMemoryPersistence)]) static let schema = SwiftData.Schema([ DataGeneration.self, Budget.self ]) }
4
0
377
Jun ’25
How to use SwiftData with MVVM architecture?
Hello all! I'm still fairly new to SwiftData, but have recently integrated it into one of my apps which has been going great. Currently, I have this integrated into my project like so (code simplified for illustration): @main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } .modelContainer(for: Todo.self) } } struct ContentView: View { @Query(sort: \Todo.date) var todos: [Todo] @Environment(\.modelContext) var moc var body: some View { ... } } In my app, the ContentView struct then also contains business logic in the form of various methods to create and modify entities stored in SwiftData. In order to achieve better separation of concerns and make SwiftTesting the view easier, I'd love to move all this business logic code out into a view model. Unfortunately, I understand that this causes issues due to view models not having access to the SwiftUI environment, making it difficult to inject the model container into a view model and using that model to initialize queries. There are some approaches to this that I found which end up defining another layer on top of SwiftData, but I can see how doing that may result in state syncronization issues between what is stored in SwiftData and the model accessing it. Given that MVVM is so popular and widely used, is there a recommended approach of doing this in a consistent way while still preserving state safety between the SwiftData model and the view model? Thanks for any suggestions! Cheers, Robin
1
0
245
Jun ’25
CoreData lightweight migration fails on iOS 26 only — "no such column: Z_110GROUPITEMS1"
We've spent several days diagnosing a CoreData migration crash that is iOS 26-specific and reproducible 100% of the time. Posting here in case others have hit this and because we believe it's an Apple bug worth documenting. Upgrading from our App Store build (CoreData model v10) to our latest TestFlight build (model v11) crashes on iOS 26 with: NSCocoaErrorDomain / Code 134110 no such column: "Z_110GROUPITEMS1" The same upgrade path on iOS 17 and iOS 18 works perfectly. What v10→v11 changes Two new entities added alphabetically early in the alphabet One new optional boolean attribute on an existing entity One new optional to-many relationship on the same existing entity All changes are lightweight-migration compatible. We use shouldMigrateStoreAutomatically = true and shouldInferMappingModelAutomatically = true Here's what we observed Adding two entities alphabetically shifts Z_ENT numbers for all subsequent entities. A central entity (EntityA) moves from Z_ENT 110 (v10) to Z_ENT 112 (v11). It has many-to-many relationships with four other entities (EntityB, EntityC, EntityD, EntityE), all using the same inverse relationship name: groupItems. Because multiple join tables reference EntityA via the same inverse name, CoreData appends a disambiguation suffix (1, 2, etc.) to column names in each join table. In v10, the relevant join tables are Z_110ENTITYB and Z_110ENTITYC, each with a column named Z_110GROUPITEMS + a suffix. -com.apple.CoreData.SQLDebug 3 prints: ALTER TABLE Z_112ENTITYB RENAME COLUMN Z_110GROUPITEMS1 TO Z_112GROUPITEMS1 But the actual column in a fresh iOS 26 v10 store is Z_110GROUPITEMS2. Column not found → crash. iOS 17/18 is consistent: both code paths use suffix 1 for Z_110ENTITYB and 2 for Z_110ENTITYC. Migration succeeds. To confirm We opened the SQLite store from a fresh iOS 26 v10 install and inspected the schema: CREATE TABLE Z_110ENTITYB ( Z_110GROUPITEMS2 INTEGER, Z_112ENTITYB INTEGER, PRIMARY KEY (Z_110GROUPITEMS2, Z_112ENTITYB) ); Then we manually renamed Z_110GROUPITEMS2 → Z_110GROUPITEMS1 and Z_110ENTITYC.Z_110GROUPITEMS1 → Z_110GROUPITEMS2 in the SQLite file. Re-ran the app — migration succeeded. The suffixes are literally swapped between what iOS 26 creates on fresh install vs. what iOS 26's migration engine expects. Our database has over 50 entities and we never before faced such an issue. This is not the first lightweight migration we are releasing after iOS 26 and that's what puzzled us. Why now?
Replies
2
Boosts
0
Views
72
Activity
12h
Fetch data in a time range, plus extend one more entry
I’m using Swift Charts in my app to display data in a visual way. For that (as well as some sum calculations) I’d like to create a fetch request that searches for all the data in a given time range plus one older and newer entry. Building a predicate that searches for a time range is easy, but how can I tell the fetch request that I would also like to have the first entry that comes before that time range and the first that comes after? I don’t know when these entries appear, so I can’t extend the time range. Example Let’s say I have the following data (timestamp – value): Feb 1, 2024 – 20 Oct 1, 2024 – 30 Jan 1, 2025 – 40 Sep 1, 2025 – 50 May 1, 2026 – 60 Oct 1, 2026 – 70 Now I want to search for the range Jan to Dec 2025. How can I retrieve the two entries that are in the range (No. 3 & 4) as well as the first entry before (No. 2) and the first after that (No. 5)? Without knowing the timestamp of No. 2 and 5. I’d appreciate some best practices or tips how to handle this special case. 🙏
Replies
1
Boosts
0
Views
58
Activity
12h
SwiftData predicate filtered by enum case
I have several Swift Data types with a property of type enum. Whenever I've tried to write a predicate returning data objects only of a certain enum case, the compiler throws an error from the macro at build time. (which I don't have handy, sorry...). Is this supported? And if so, how would you write the predicate? @Model public final class AlbumList { // ... public var listType: AlbumListType // ... } public enum AlbumListType: String, CaseIterable, Codable { case listener case dj }
Replies
2
Boosts
0
Views
52
Activity
12h
Migration Hash Mismatch
I'm migrating an existing Core Data app to SwiftData and encountering a persistent schema hash mismatch that prevents the store from loading. The Core Data store has been in production for years with version 4 of the model. I have two models, lets call the Crew and Astronaut Create is a parent in the to-many relationship to Astronaut, the child. When I try the migration, the Crew model‘s hash matches, but the Astronaut model’s mismatches. I also tried changing relationship parameters; deleteRule, maximumModelCount, inverse, originalName, and hashModifier. hashModifier changes the hash, but to a third value, so it does not appear to be a way to match the existing Core Data hash. So, is there a supported way for SwiftData to generate an optional to-one relationship that is Core Data hash-compatible with optional=YES minCount=1 maxCount=1? Or is a migration/rehash/copy step required before SwiftData can adopt this V4 store in place?
Replies
3
Boosts
0
Views
46
Activity
12h
SwiftData + public CloudKitDatabase?
Greetings, Thank you for SwiftData + new features. I am looking forward to the SwiftData Group Lab on Friday. How does the performance of SwiftData + CloudKit private database compare to CoreData + NSPersistenceCloudKitContainer? Also, will SwiftData + CloudKit support the CK public database, in the future? Thank you!
Replies
2
Boosts
2
Views
64
Activity
12h
Dynamic Compound Predicates
This is relating to the question I have for the App Intents framework (see my question). I know that SwiftData started to support compound predicates with macOS 14.4/iOS 17.4 or later. But from what I understand they are not dynamic and are validated at compile time. Is there a way to update/construct predicates while the app is running? For example to create a search tab that allows searching and filtering for my items in the app.
Replies
1
Boosts
0
Views
49
Activity
13h
889 CoreData errors in a SwiftData app of less than 30 lines
I have created a SwiftData iOS app from Paul Hudson's Hacking With Swift series in order to troubleshoot some SwiftData issues I am having in a separate app. I have raised > FB22925785 I have gone back to basics to try and problem solve so I have used the first part of an app of Paul's from [https://www.hackingwithswift.com/quick-start/swiftdata/defining-a-data-model-with-swiftdata] The App is very simple and only contains the following import SwiftData import SwiftUI @main struct iTour_from_scratchApp: App { var body: some Scene { WindowGroup { ContentView() .modelContainer(for: Destination.self) } } } The data is import SwiftData @Model class Destination { var name: String var details: String var date: Date var priority: Int init(name: String, details: String, date: Date, priority: Int) { self.name = name self.details = details self.date = date self.priority = priority } } The view is import SwiftUI struct ContentView: View { var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("Hello, world!") } .padding() } } #Preview { ContentView() } When I try to run this, I am getting 889 lines of CoreData errors appearing in the Console and they start with CoreData: error: Failed to stat path '/Users/chrissantavy/Library/Developer/CoreSimulator/Devices/913BFD87-5FD8-47B9-AD0C-81238E74E89E/data/Containers/Data/Application/29586FDF-F08D-48E8-AF58-FD7BD5A30525/Library/Application Support/default.store', errno 2 / No such file or directory. CoreData: error: Executing as effective user 501 CoreData: error: Executing as effective user 501 CoreData: error: Sandbox access to file-write-create denied CoreData: error: Sandbox access to file-write-create denied CoreData: error: Failed to statfs file; errno 2 / No such file or directory. CoreData: error: Failed to statfs file; errno 2 / No such file or directory. CoreData: error: Information for file system CoreData: error: Information for file system CoreData: error: --------------------------- CoreData: error: --------------------------- CoreData: error: File system type: 0 CoreData: error: File system type: 0 CoreData: error: File system flags: 0 CoreData: error: File system flags: 0 CoreData: error: Total data blocks: 0 CoreData: error: Total data blocks: 0 CoreData: error: Free data blocks: 0 CoreData: error: Free data blocks: 0 CoreData: error: Free blocks for nonsuperuser: 0 CoreData: error: Free blocks for nonsuperuser: 0 CoreData: error: Total i-nodes: 0 CoreData: error: Total i-nodes: 0 CoreData: error: File system ID: 0, 0 CoreData: error: File system ID: 0, 0 CoreData: error: Free i-nodes: 0 CoreData: error: Free i-nodes: 0 CoreData: error: Owner UID: 0 CoreData: error: Owner UID: 0 CoreData: error: Filesystem type name: CoreData: error: Filesystem type name: CoreData: error: Mount on name: CoreData: error: Mount on name: CoreData: error: Mount from name: CoreData: error: Mount from name: CoreData: error: Failed to stat path '/Users/chrissantavy/Library/Developer/CoreSimulator/Devices/913BFD87-5FD8-47B9-AD0C-81238E74E89E/data/Containers/Data/Application/29586FDF-F08D-48E8-AF58-FD7BD5A30525/Library/Application Support', errno 2 / No such file or directory. CoreData: error: Failed to stat path '/Users/chrissantavy/Library/Developer/CoreSimulator/Devices/913BFD87-5FD8-47B9-AD0C-81238E74E89E/data/Containers/Data/Application/29586FDF-F08D-48E8-AF58-FD7BD5A30525/Library/Application Support', errno 2 / No such file or directory. CoreData: error: Executing as effective user 501 The full list of errors is attached iTour from scratch Part-1 errors.txt
Replies
1
Boosts
0
Views
69
Activity
13h
SwiftData: class inheritance without entity inheritance
In SwiftData, how can I have class inheritance without entity inheritance? My understanding is when models are subclassed they then share the same SQLite table allowing one Query to return multiple model types. However what if we don't require that functionality and only wish to share some overridable logic. I.e. share a base class implementation with multiple @Model subclasses but each model is in it's own SQLite table. E.g. say I want a savedAt timestamp field in every model and have the logic for setting it across all models. Or do some validation that calls super to check parent implementation is valid too. Maybe the validation behaviours can be done using protocols and extensions some how? Thanks!
Replies
1
Boosts
0
Views
48
Activity
13h
Sectioned fetching: beyond String keyPaths?
Hello, world! First and foremost, thank you for all the enhancements made to SwiftData and kudos to the team! In Thomas’ video demonstrating “What’s new in SwiftData”, I notice the keyPath (trip.destination) used to create sections in the list is a String. I can’t help but wonder if a keyPath of another data type could be used (like trip.startDate), were one to want to create sections based on whether a trip is past or upcoming for example? Thank you in advance for your much appreciated input! Zoé
Replies
1
Boosts
0
Views
66
Activity
13h
Aggregate functions in SwiftData
Hi, does SwiftData supports aggregate functions through NSExpression for operations like SUM, AVG, MIN, and MAX?
Replies
1
Boosts
0
Views
62
Activity
13h
SwiftData @Query with a Custom (non-standard) Sort Criterion
I have a @Query which I would like to sort using a custom criterion. I would like to use an array of SortDescriptors where I can specify a key path and a CUSTOM SortComparator. (I.e., I can write SortComparators which do what I want, but I can't use them in a SortDescriptor because there is no initializer which takes a SortCompartor which is not a String.StandardComparator.) This is not a question about dynamic sorting, which has well-known solutions. I am trying to perform a sort which cannot be satisfied by any existing SortDescriptor (or array of SortDescriptors). Any thoughts about how to resolve this situation?
Replies
2
Boosts
0
Views
73
Activity
5d
SwiftData 'simple' migration failing
This is a long post, so let me start with a summary: I am attempting to implement what "ought to be" a simple SwiftData migration, and am receiving the following fatal error from the ModelContainer initializer: NSCocoaErrorDomain Code=134504 "Cannot use staged migration with an unknown model version." The crash occurs both in the Simulator and on a physical device. Both the original schema and the new schema load and run as expected if loaded from scratch — so I conclude the Models are OK; it is the migration from the original schema to the new schema which is the issue. I have reported this as FB22652791 and Technical Incident Case # 19893980. I have two model projects available — one contrived, the other using my actual SwiftData models. Now the Details I am developing a SwiftUI/SwiftData app. I am (currently) using Xcode 26.5-beta-3. I set up an alpha-test build using the following approach: public class DatabaseSchema { public let dbSchema: Schema = Schema([ Model1.self, ... , Model16.self ], version: Schema.Version(0, 9, 0)) public var modelContainer: ModelContainer { let container: ModelContainer let modelConfiguration = ModelConfiguration(schema: dbSchema, isStoredInMemoryOnly: false) do { container = try ModelContainer(for: dbSchema, migrationPlan: nil, configurations: [modelConfiguration]) } catch { fatalError("Failed to creae model conainer") } return container } This defines database version 0.9. For version 1.0, I made three changes to the database: added an attribute of type String to Model2. added three attributes of type [Struct], where Struct conforms to Codable, Equatable and Hashable to Model3, and added a new model (which I'll call Model17) I define two schemas: public enum Schema090: VersionedSchema { public static var versionIdentifier = Schema.Version(0, 9, 0) public static var models: [any PersistentModel.Type] = [ Model1.self, Schema090.Model2.self, Schema090.Model3.self, ... ] } and public enum Schema100: VersionedSchema { public static var versionIdentifier = Schema.Version(1, 0, 0) public static var models: [any PersistentModel.Type] = [ Model1.swift, Schema100.Model2.self, Schema100.Model3.self, ..., Model16.self, Schema100.Model17.self ] } For models that changed, I use the following approach: public typealias Model3 = Schema100.Model3 extension Schema090 { @Model final class Model3 { ... } public init() { ... } } extension Schema100 { @Model final class Model3 { ... <added attributes, initialized> } public init() { ... } } The DatabaseSchema class was modified as follows: public class DatabaseSchema { public let dbSchema: Schema = Schema([ Model1.self, Schema100.Model2.self, Schema100.Model3.self, ... , Model16.self, Schema100.Model17.self ], version: Schema.Version(1, 0, 0)) public var modelContainer: ModelContainer { let container: ModelContainer let modelConfiguration = ModelConfiguration(schema: dbSchema, isStoredInMemoryOnly: false) do { container = try ModelContainer(for: dbSchema, migrationPlan: MigrationPlan.self, configurations: [modelConfiguration]) } catch { fatalError("Failed to creae model conainer") } return container } where the migration plan is the trivial custom migration that makes sure that all added attributes of existing records are properly initialized. enum MigrationPlan: SchemaMigrationPlan { static var schemas: [any VersionedSchema.Type] = [ Schema090.self, Schema100.self ] static var stages: [MigrationStage] = [version090ToVersion100] static let version090ToVersion100 = MigrationStage(fromVersion: Schema090.self, toVersion: Schema100.self, willMigrate: { _ in }, didMigrate: { context in let models = try context.fetch( FetchDescriptor<Schema100.Model3>()) for model in models { < initial the added attributes > { try context.save() }) } This is all simple stuff. Nothing particularly fancy here. But running this code always crashes in the ModelContainer initializer. In my two sample projects, I get two different error messages — in the contrived example, the error message is Code=134110 "An error occurred during persistent store migration." reason=Cannot migrate store in-place: Validation error missing attribute values on mandatory destination attribute, ... and in the sample project that uses my actual data model, I get NSCocoaErrorDomain Code=134504 "Cannot use staged migration with an unknown model version." My Thoughts Since obviously most folks are doing SwiftData migrations without the problems I am experiencing, the obvious possibilities are I'm doing something stupid that I just don't see. There is a problem because the original schema was given a version value of Schema.Version(0, 9, 0). (i.e., major version number was 0) There is a problem because I am adding an attribute of type [Struct] where Struct is Codable, Hashable, and Equatable. I.e., migration isn't working properly with attributes which are stored as their codable representations. Or maybe something else? In any case, any help you can offer would be greatly appreciated.
Replies
7
Boosts
0
Views
651
Activity
1w
TestFlight build crashes from fetch descriptor
I have a FetchDescriptor that uses starts(with:) which works fine in debug builds but crashes in TestFlight and archive. For background information I'm using iCloud and model inheritance where the property being used in fetch descriptor is defined on the superclass, the fetch descriptor is for the subclass. Implementation: static func fetchDescriptor(nameStartingWith prefix: String) -> FetchDescriptor<ColorAsset> { let predicate = #Predicate<ColorAsset> { asset in asset.name.starts(with: prefix) } return FetchDescriptor<ColorAsset>(predicate: predicate) } @Model public class Asset: Identifiable { // MARK: - Properties var name: String = "" .... } @available(macOS 26.0, *) @Model public class ColorAsset: Asset { ... }
Replies
4
Boosts
1
Views
415
Activity
1w
SwiftData crash on new property (Could not cast...)
I have a small example where adding a new property to a persisted Codable struct causes a crash on launch instead of decoding the missing property using its default value. Steps Run this app once and press "Insert Event" to persist data: import SwiftUI import SwiftData @main struct SwiftDataCrash: App { var body: some Scene { WindowGroup { ContentView() } .modelContainer(for: Event.self) } } struct ContentView: View { @Environment(\.modelContext) private var modelContext @Query private var events: [Event] var body: some View { VStack(spacing: 12) { Text("Events: \(events.count)") Button("Insert Event") { let event = Event( recurrence: Recurrence( interval: 1 ) ) modelContext.insert(event) try? modelContext.save() } List(events) { event in Text(String(describing: event.recurrence)) } } .padding() } } @Model final class Event { var recurrence: Recurrence? = nil init(recurrence: Recurrence? = nil) { self.recurrence = recurrence } } struct Recurrence: Codable { var interval: Int // STEP 2: // After first run + inserting an Event, uncomment this and run again. // Expected: old data decodes with default [] // Actual: SwiftData may crash while reading Event.recurrence // // var exceptionDates: [Date] = [] } Then uncomment: var exceptionDates: [Date] = [] and run again without deleting the store. Actual result App crashes on launch with: Could not cast value of type 'Swift.Optional<Any>' to 'Swift.Array<Foundation.Date>' The crash appears to happen inside generated SwiftData persisted-property getter code. Expected result I expected the old persisted Recurrence values to decode with: exceptionDates == [] Is this expected behavior or a SwiftData bug?
Replies
2
Boosts
0
Views
258
Activity
1w
Crash when adding large JSON data to SwiftData: Could not cast value of type '__NSCFBoolean' to 'NSString'
Within the app main struct, the SwiftData modelContainer is created by fetching and decoding JSON from remote site. The decoded data is inserted into the modelContext. Immediately thereafter, the app crashes with the error Could not cast value of type '__NSCFBoolean' to 'NSString' . I have run the JSON from the site through JSON lint and it is correctly structured. The JSON data is structured as follows: Overview object which includes a Result object which includes a List Array. Each List object includes a Books Array. I have run the app through AI and the result indicates that there is data corruption when adding the data to the database. The AI rewrote the code such that the book data is encoded into a data blob and then adds the data to the database, instead of adding the individual books (which is extensive.) This approach works without crashing, but I would like to find out if this is indeed a SwiftData issue.
Replies
4
Boosts
0
Views
168
Activity
Jul ’25
SwiftData App Crashes only in TestFlight...
I have been building a SwiftUI/SwiftData app for some time that has been working fine in both the simulator and devices I have loaded from Xcode. These include two iPhones an iPad and a VisionPro. I have gotten to the point where I wanted to do some external beta testing so uploaded the App to the AppStore and then had a couple of users download via TestFlight. Unfortunately the App crashes and I have been unable to figure out why based on the feedback that I have gotten. The app has 4 Models that are linked in a fairly straightforward manner. There is a Habit class that is the central object. An Aspiration class that may have several Habits associated with it. An Anchor Class and a Celebration Class both attach to a Habit. I was able to get my neighbor to use TestFlight to download the App and it crashed when trying to insert a Habit but Aspirations, Anchors and Celebrations work fine and can be added, stored and deleted without issue. I then had my neighbor connect their phone to my computer and I download the app directly and it works fine as expected even when not connected to the debugger so clearly something is different between the two environments and I am at a loss so could use someone with good knowledge to help me figure this out.
Replies
4
Boosts
0
Views
189
Activity
Jul ’25
How to handle completely adopting SwiftData when you need all the lightweight migrations defined in the xcdatamodeld?
At the end over every "how to adopt SwiftData in a CoreData app" it says, now go ahead and delete the xcdatamodeld files. But my xcdatamodeld contains all the migrations necessary to even support SwiftData in the first place. If I have 10 versions of my CoreData model, and the 10th matches the first version of my SwiftData model... But a user is on the 7th version, and then upgrade to the new app that doesn't contain any xcdatamodeld files.. How are you actually supposed to handle this? I don't want to keep supporting both. I just want to use SwiftData.
Replies
0
Boosts
0
Views
187
Activity
Jun ’25
SwiftData superclass prevents usage of ID
New subclassing is a great addition to SwiftData, while trying to utilize the superclass type for selection state I’m seeing the following error: @available(macOS 26.0, *) @Model public class Asset { … } var assetSelection: [Asset.ID] = [] Error: 'ID' is inaccessible due to '@_spi' protection level Replacing the type with a subclassed swift data model of Asset works, but to handle mixed selection and the new .dragContainer modifier I need to be able to use the superclass. Is this intended behavior?
Replies
7
Boosts
0
Views
247
Activity
Jun ’25
Add App Group to Existing SwiftData App
I have an existing app that uses SwiftData and now want to add widgets. I added the widget extension, created an App Group to use for the main app target and widget targets and successfully created the widget. However, when testing the updates I often experience data loss - as though the including the widget extension is creating a new instance of modelContainer. Am I missing something to ensure there won't be any data loss when adding the App Group and widget extension? For additional context: I’ve followed the Backyard Birds example code except that it uses a separate app package. My app does not use an external app package, but I am using some elements of the DataGeneration file. My files containing the SwiftData models have Target Memberships for both the main app target and widget extension target. In the TimelineProvider for my widgets, I'm doing the following: let modelContext = ModelContext(DataGeneration.container) init() { DataGeneration.generateAllData(modelContext: modelContext) } My DataGeneration file (simplified) is as follows. When adding the widget target, I sometimes see the log for "Creating instance of DataGeneration". import Foundation import SwiftData @Model class DataGeneration { var requiresInitialization: Bool = true init(requiresInitialization: Bool = true) { self.requiresInitialization = requiresInitialization } private func generateInitialData(modelContext: ModelContext) { if requiresInitialization { let budget = Budget() modelContext.insert(budget) requiresInitialization = false } } private static func instance(with modelContext: ModelContext) -> DataGeneration { if let result = try! modelContext.fetch(FetchDescriptor<DataGeneration>()).first { logger.info("Found instance of DataGeneration") return result } else { logger.info("Creating instance of DataGeneration") let instance = DataGeneration() modelContext.insert(instance) return instance } } static func generateAllData(modelContext: ModelContext) { let instance = instance(with: modelContext) instance.generateInitialData(modelContext: modelContext) } } extension DataGeneration { static let container = try! ModelContainer(for: schema, configurations: [.init(isStoredInMemoryOnly: DataGenerationOptions.inMemoryPersistence)]) static let schema = SwiftData.Schema([ DataGeneration.self, Budget.self ]) }
Replies
4
Boosts
0
Views
377
Activity
Jun ’25
How to use SwiftData with MVVM architecture?
Hello all! I'm still fairly new to SwiftData, but have recently integrated it into one of my apps which has been going great. Currently, I have this integrated into my project like so (code simplified for illustration): @main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } .modelContainer(for: Todo.self) } } struct ContentView: View { @Query(sort: \Todo.date) var todos: [Todo] @Environment(\.modelContext) var moc var body: some View { ... } } In my app, the ContentView struct then also contains business logic in the form of various methods to create and modify entities stored in SwiftData. In order to achieve better separation of concerns and make SwiftTesting the view easier, I'd love to move all this business logic code out into a view model. Unfortunately, I understand that this causes issues due to view models not having access to the SwiftUI environment, making it difficult to inject the model container into a view model and using that model to initialize queries. There are some approaches to this that I found which end up defining another layer on top of SwiftData, but I can see how doing that may result in state syncronization issues between what is stored in SwiftData and the model accessing it. Given that MVVM is so popular and widely used, is there a recommended approach of doing this in a consistent way while still preserving state safety between the SwiftData model and the view model? Thanks for any suggestions! Cheers, Robin
Replies
1
Boosts
0
Views
245
Activity
Jun ’25