Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

SwiftData

RSS for tag

Discuss how to write your model code declaratively to add managed persistence and efficient model fetching.

SwiftData Documentation

Posts under SwiftData subtopic

Post

Replies

Boosts

Views

Activity

Is SwiftData missing some APIs?
I was taking a look through SwiftData documentation for any changes coming from WWDC 2025 regarding my previous issues that was left unaddressed… KeyPaths are still not provided in Schema.Attribute as it was with Schema.Relationship. I also don’t see an initializer for HistoryTombstone, making it impossible to set up HistoryDelete protocol from what I can gather. I would appreciate a confirmation that we have been provided everything we need to complete the custom store, because I don’t know if everything I need has been provided or if some APIs have not been opened up. Thank you.
0
0
9
3h
SwiftData Predicate for optional to-many (as required by CloudKit) relationships crashes
Fails with "to-many key not allowed here" // parent.children?.contains(where: { // $0.name == "Abbiejean" // }) != nil parent.children.flatMap { children in children.contains(where: { $0.name == "Abbijean" }) } == true How are we supposed to query on relationships? This is a huge problem. This is a major limitation blocking migration of CoreData to SwiftData. We can do this with NSPredicate: let moodAnalysis = NSPredicate(format: "ANY moodAnalysis.labels.title == %@", label.description) let stateOfMinds = NSPredicate(format: "SUBQUERY(stateOfMinds, $x, SUBQUERY($x.labels, $y, $y.title == %@).@count > 0).@count > 0", label.description) The accepted answer on stack overflow is: you can't Document says that optionals are allowed in predicates The SwiftData team has made a big show of saying that we can use idiomatic swift for our predicates. But we cannot even filter on relationships when the container is backed by CloudKit... That should be a HUGE warning in the documentation. "For those of you who are considering a costly refactor from CoreData to SwiftData, and are currently using CloudKit, all relationships are mandatory optional arrays, and you can't write predicates on them"
1
0
37
1d
SwiftData Transient Macro Observability
I have a SwiftData model that includes a transient image, declared as follows: @Transient var image: UIImage? It appears that SwiftData does not track changes to transient properties and so the following view will not update when the image changes from nil to an actual image. ZStack(alignment: .topTrailing) { if let image = item?.image { Image(uiImage: image) } else { ProgressView() } } Ideally, the SwiftData model would still observe changes in transient properties and just not persist them. As such, other code that works with observable objects would work as otherwise expected.
0
0
25
1d
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 'ReferenceWritableKeyPath<GuruSchemaV2.Rubric, Bool>' does not conform to the 'Sendable' protocol; this is an error in the Swift 6 language mode Here is an example predicate, from a static method on the Rubric type: static func notArchived() -> Predicate<Rubric> { return #Predicate<Rubric> { rubric in !rubric.archived } } And the error highlights line 5 of the expanded macro: Foundation.Predicate<Rubric>({ rubric in PredicateExpressions.build_Negation( PredicateExpressions.build_KeyPath( root: PredicateExpressions.build_Arg(rubric), keyPath: \.archived ) ) }) What is the correct way to reference properties of a model type using #Predicate?
0
1
31
3d
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?
6
0
91
4d
SwiftData Models not working after updating to macOS 26
Im working on an app which have a lot of diffrent models which are having relationships one to many and so on and on macos Sequoia and Sonoma everything is working but on Tahoe i have this error SwiftData/SchemaProperty.swift:286: Fatal error: Illegal attempt to create a property that's a sequence of a non-codable type (_buffer - _ArrayBuffer). Did you mean to use a transformable attribute? I also use computed properties to perform login on model value change like property name is var name: String{ get:{ self._name} set:{ self._name = $0 } } var _name: String = "" I no where use ArrayBuffer i just use [Double] it its needed
3
0
66
5d
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
0
0
39
6d