801 questions
0
votes
0
answers
57
views
Container Failing to Initialize After a Successful Migration & Initialization
I'm experiencing the following error with my SwiftData container when running a build:
Code=134504 "Cannot use staged migration with an unknown model version."
Code Structure - Summary
I ...
2
votes
1
answer
81
views
SwiftData update causes render with initial data overwriting changes
I'm working on a SwiftUI app using SwiftData, and I'm very new to both frameworks, and swift in general to be honest. I'm trying to create a webview that embeds a monaco editor that will then send the ...
0
votes
0
answers
27
views
How to assign a different ModelContainer to @Query? [duplicate]
My application contains two different databases. The first contains information that does not change and will be distributed in the app's bundle. The second contains information that can be edited and ...
-12
votes
1
answer
249
views
iOS app crashing in simulator after SwiftData refactor [closed]
I've been working on a small, SwiftUI/SwiftData app for personal usage. This has been going well until yesterday when I decided I should refactor the SwiftData side of things (split some tables up, ...
Best practices
0
votes
9
replies
122
views
Is there any way to improve SwiftData sort performance for its array variables?
I have two SwiftData models like below:
@Model
final class SDStock {
init() {}
var id = UUID()
var title = "abc"
var code = "123456"
@Relationship(deleteRule: ....
0
votes
1
answer
80
views
No exact matches in call to instance method 'setValue' for custom property
I am trying to use a custom property inside a SwiftData model:
import SwiftData
struct Foo {
var name: String
}
@Model
final class Bar {
var id: String
var name: String
var foo: Foo
...
1
vote
2
answers
116
views
SwiftData: context.insert() doesn’t persist data [closed]
I’m new to iOS development and currently learning to persist data using SwiftData.
I’m building a very simple to-do list app to understand how local persistence works.
However, I’ve hit a wall and ...
2
votes
1
answer
229
views
Why does @Model conformance to PersistentModel and Hashable cross into @MainActor isolation in Swift 6.2?
I’m using Swift 6.2 with MainActor set as the default global actor in my project.
When I create a SwiftData @Model that conforms to a protocol extending PersistentModel, I get actor isolation errors ...
1
vote
1
answer
79
views
Where to put the @Relation(inverse) annotation and what are the benefits? [closed]
Obviously it isn't allowed to put the @Relation(inverse)-annotation on both models. Otherwise one receives an error-message about "Circular Reference".
Therefore, should one put the ...
0
votes
1
answer
75
views
Swift binding to dictionary value
I'm working on an app where I have a view AddMonthView where you put in the monthly balance for financial accounts. That account list is dynamic. So I spent some time trying to figure out how to bind ...
0
votes
0
answers
180
views
Issue with SwiftData inheritance (iOS 26)
Every time I insert a subclass (MYShapeLayer) into the model context, the app crashes with an error:
DesignerPlayground crashed due to fatalError in BackingData.swift at line 908. Never access a full ...
1
vote
1
answer
116
views
How to make a document based app with SwiftData
I have an iOS app that uses SwiftData with @Model types. I’m porting it to macOS and want a document-based app where each window is its own document (like TextEdit). I understand the basic document ...
0
votes
1
answer
125
views
ModelContainers failing to initialize
I need help debugging why my app is failing on hardware trying to initialize the ModelContainer. This is my first app so I may be missing something obvious, but I have two models that I'm trying load, ...
0
votes
0
answers
88
views
Migrating Schema to VersionSchema
I'm very new to SwiftData and have run into an issue where I need to change a datatype in a model. I've attempted to implement a migration plan but my original schema was not of type VersionedSchema ...
1
vote
1
answer
78
views
Reactive global count of model items without loading all records
I need a way to keep a global count of all model items in SwiftData.
My goal is to:
track how many entries exist in the model.
have the count be reactive (update when items are inserted or deleted).
...
0
votes
1
answer
245
views
Set a .modelContext in view's environment to use Query
I started getting this error, and I can't figure out why. I have attached a modelContainer to my main WindowGroup, and I have other views that use the @Query property and they work fine, but for some ...
1
vote
1
answer
79
views
How can I use a model from DocumentGroup alongside one from .modelContainer in Swift Data?
I’m building a document‑based Swift UI app using DocumentGroup with SwiftData. I want:
A document‑scoped model (Gate) that is persisted inside a file
An app‑scoped model (User) that is stored in the ...
1
vote
1
answer
76
views
Predicates with generics
I’m trying to use Predicate with generics but I keep getting the following compilation error :
Cannot convert value of type 'PredicateExpressions.Equal<PredicateExpressions.ConditionalCast<...
1
vote
0
answers
70
views
Sync not working for independent watchOS app using CloudKit and SwiftData
I'm working on an independent watchOS app and I was looking for the best solution to persist data locally and have an online sync with the iCloud storage.
I've found out about the SwiftData which is ...
0
votes
1
answer
82
views
SwiftData @Model not persisting across app launches (works only in memory)
I’m using SwiftData in a macOS app (Xcode 16, macOS 15, Swift 6).
My models and managers compile fine, inserts/deletes work at runtime, but nothing persists after relaunch — all data is gone.
Here’s a ...
0
votes
0
answers
44
views
Swift 6: “Main actor-isolated conformance of FeelingType to Encodable cannot be used in nonisolated context” with SwiftData @Model [duplicate]
I’m migrating a small SwiftData model from Swift 5 to Swift 6. The same code compiles under Swift 5, but with Swift 6 it fails when the @Model macro expands a property whose type conforms to Codable.
...
1
vote
2
answers
98
views
SwiftUI TextField input is super laggy for SwiftData object
I have a SwiftUI View where I can edit financial transaction information. The data is stored in SwiftData. If I enter a TextField element and start typing, it is super laggy and there are hangs of 1-2 ...
4
votes
2
answers
386
views
How do I refresh a SwiftData (manual) fetch whenever the database is changed?
Summary
I have a View Model class, paired with a SwiftUI View, that fetches filtered entries from SwiftData using a ModelActor. I've used SwiftUI's .task(id:) to trigger re-loads in the View Model ...
2
votes
1
answer
82
views
Setting the deleteRule to .cascade in @relationship and getting an error after deleting the Model
I am practicing the @Relationship(deleteRule:) feature in SwiftData, and I encounter a crash when deleting a model under certain circumstances.
Error message
Cannot fulfill future for ...
0
votes
2
answers
246
views
Inheritance in SwiftData with iOS 26.0 and Xcode beta
Hello, I'm trying to create a simple app with inheritance and SwiftData. Unfortunately, I keep getting the same error with this example:
Thread 1: Fatal error: Never access a full future backing data ...
0
votes
0
answers
72
views
SwiftUI DocumentGroup navigation behaviour - bombs back to the document picker
A clean Xcode template for a multi-platform document app using SwiftData as storage produces something that looks like this…
... which is obviously daft. See below for how to fix the look, but my ...
0
votes
0
answers
74
views
SwiftData External Storage Option iCloud Sync
In my SwiftData model one of the properties is set to Data? with externalStorage option. So, it means that it will not be stored in the actual database but a reference will be in the database and file ...
0
votes
1
answer
87
views
How can I synchronize a Swift enum with a SwiftData lookup table?
Motivation
Although recent versions of SwiftData allow us to store enum fields on our models, the underlying database representation is an opaque blob. Sorting and filtering on the field yields a ...
1
vote
1
answer
211
views
How Can I Retrieve All SwiftData Values at Runtime?
I'm working with SwiftData and trying to replicate behavior similar to what I used to do with CoreData, where I had an extension on NSManagedObjectContext that allowed me to fetch all stored objects, ...
0
votes
2
answers
164
views
List won't update when item is deleted
I have two selectable lists, the first is populated with Category model objects and the second with Subcategory model objects (which are taken from selected Category value of the first list).
When I ...
1
vote
1
answer
367
views
Generic parameter 'V' could not be inferred while ForEach in sectioned results
I'm using the swiftdata-sectionedquery package. It basically allows SwiftData to have a SectionedFetchRequest. The problem I am running into is I keep getting this error whenever I try to foreach on ...
0
votes
1
answer
165
views
SwiftData runtime crash using Predicate macro with protocol-based generic model
I'm working with SwiftData and trying to share logic across multiple models using protocols and protocol extensions.
I’ve created some common protocols like Queryable, StatusRepresentable, and ...
0
votes
0
answers
78
views
Why doesn’t onChange(of: @Query results) fire when a SwiftData model’s property changes?
Problem Description
When using SwiftUI's @Query with SwiftData models, I encountered confusion about reactivity triggering. In certain situations, SwiftUI's reactivity mechanism doesn't work as ...
0
votes
0
answers
27
views
How to use Enums in Predicates with @Query and SwiftData [duplicate]
So, I have this code:
import SwiftUI
import SwiftData
enum MyEnum: String, CaseIterable, Codable {
case first = "first case"
case second = "second case"
case third = &...
0
votes
0
answers
46
views
Why @Query ignores search term? [duplicate]
I have this code:
import SwiftUI
import SwiftData
@Model
final class Item {
@Attribute(.unique) var id = UUID()
private(set) var name: String
init(name: String) {
self.name = ...
-1
votes
1
answer
103
views
Proper way to pass a class around SwiftUI views [closed]
I have a SwiftData class that I get with @Query private var items: [Item] in a SwiftUI view. Then I pass it to a sub view that passes it to other sub views. What is the best way to pass the class ...
0
votes
0
answers
114
views
I am getting sporadic crashes since updating Swift ModelContainer
I have some models in my app:
[SDPlanBrief.self, SDAirport.self, SDChart.self, SDIndividualRunwayAirport.self, SDLocationBrief.self]
SDLocationBrief has a @Relationship with SDChart
When I went live ...
0
votes
0
answers
83
views
Optimized saving for TextEditor using SwiftData
I have notes app using SwiftData also using CloudKit and the note editor is taking 125% CPU usage.
For example this is the note model:
@Model Class Note {
var title: String = ""
var ...
0
votes
0
answers
32
views
Predicate Error: "Captured/constant values of type 'xxx' are not supported" [duplicate]
I'm working with SwiftData and trying to filter my data using predicates with a custom enum, but I'm getting an error when the predicate executes:
SwiftDataError(_error: SwiftData.SwiftDataError....
0
votes
0
answers
86
views
Have I messed up my Swift Data Versioned Schema?
I have 5 Swift Data models:
SDPlanBrief, SDAirport, SDChart, SDIndividualRunwayAirport, SDLocationBrief
I went live with my first release of the app, with no migration plan or versioned schemas. ...
3
votes
1
answer
53
views
SwiftUi error when trying to navigate to a view with a SwiftData model context
I'm doing the SwiftData tutorial on Hacking With Swift, and I'm trying one of the challenges which is to add a swipe to delete on a list of related models. I thought I had the right idea, but I'm ...
-3
votes
1
answer
130
views
private(set) var _X: SwiftData.Query<C.Element, C> = .init() error
Modified to answer concerns appearing in comments.
Have the following code:
import SwiftUI
import SwiftData
@main
struct Fu: App {
var body: some Scene {
WindowGroup {
...
1
vote
0
answers
54
views
NSPersistentHistoryToken to DefaultHistoryToken in SwiftData
Is it possible to convert a NSPersistentHistoryToken to a DefaultHistoryTransaction.TokenType (DefaultHistoryToken)?
func subscribeToModelChanges() async {
NotificationCenter.default.notifications(...
3
votes
1
answer
519
views
SwiftData #Predicate in Swift 6 language mode
I'm trying to migrate over to the Swift 6 language mode, but the biggest issue I'm facing is that any use of SwiftData #Predicate or SortDescriptor results in this warning from the compiler:
Type '...
0
votes
1
answer
137
views
Updating view when SwiftData model changes
Edit with minimal reproducible example:
@main
struct SwiftDataTestApp: App {
var sharedModelContainer: ModelContainer = {
let schema = Schema([
Team.self, Player.self
])...
0
votes
0
answers
48
views
Why is Predicate macro in Swift not working? [duplicate]
I am working on a wallet application and I have the models transaction and wallet. Transaction has an extension enum Transaction.Kind which has case deposit and withdrawal. I want to filter the ...
0
votes
0
answers
105
views
ShieldConfigurationExtension & SwiftData
I am developing a Screen Time App for iOS and I am having issues with the ShieldConfigurationExtension (ShieldConfigurationDataSource). I know this extensions is sandboxed but I should be able to read ...
0
votes
1
answer
725
views
Perform SwiftData operations with @ModelActor on a background thread
So I don't know how to perform SwiftData operations on a background thread. Every time I call my @ModelActor, everything runs on the main thread. I've read online that you must instantiate it on a ...
1
vote
1
answer
361
views
How to completely reset SwiftData programatically?
I'm currently developping an app, which runs on several devices via TestFlight. Since I am still in an early stage my @Models are subject to change quite frequently.
I know that the preferred way of ...
1
vote
1
answer
162
views
SwiftData - how to empty a database?
How do you "empty" a database in SwiftData?
Something like?
func DeleteTestDatabase() {
@Environment(\.modelContext) var context
do {
try context.delete(model: ...