Swift Data is a data modeling framework from Apple for use in apps running on their operating systems, including iOS, macOS, and others. It's closely related to Core Data but is used in different, more Swift-friendly ways.
Questions tagged [swift-data]
92 questions
1
vote
1 answer
SwiftData, how to update UI after delete a model
After deleting all objects in Group, how to update UI ('reload' Query) ?
My function to delete all Group objects, in a Button action in toolbar :
private func deleteAll() {
print("delete all!")
do {
if try…

u0cram
- 89
- 6
1
vote
1 answer
How do I specify a custom sort order for @Query in SwiftData?
I am trying to use SwiftData for an application I was using CoreData for. When I specify a sort order based on a String type, it gives me a list sorted with case-sensitivity:
If my model looks like:
import SwiftData
@Model
final class Item {
…

Syd Polk
- 73
- 1
- 5
1
vote
1 answer
Swiftdata Fatal error: Composite Coder only supports Keyed Container
When I try to instantiate a ModelContainer object, I get an error. The error is like this:
Thread 1: Fatal error: Composite Coder only supports Keyed Container.
SingleFolder is a custom model.
This is my code:
class DatabaseService {
static…

ikunhub
- 73
- 1
- 7
1
vote
1 answer
How to fetch with new #Predicate macro by Enum property
I'm trying to fetch some results with the new #Predicate macro. The problem appears when I try to use an enum property inside the predicate. Does anyone know if it can be done or if this is a bug?
XCode 15 beta 4
Fetch 1:
let predicate =…

PerroVerde
- 49
- 8
1
vote
1 answer
SwiftData relations
I'm I'm playing with the new swiftData I'm try to understand how the relations work my happy to have to model class:
Each Flight on my APP contained many crews, I wrote the relation like in the following code but keep getting the error " Fatal…

Damiano Miazzi
- 1,514
- 1
- 16
- 38
1
vote
0 answers
How to create a Predicate macro that filters a PersistentModel according to the number of characters contained in a String property?
Let's create a PersistentModel called Item.
import Foundation
import SwiftData
@Model final class Item {
var text: String
init(text: String) {
self.text = text
}
}
When I try to filter items according to the number of…

parapote
- 433
- 2
- 8
1
vote
0 answers
SwiftData crashes when setting optional String of Codable property
I've run into an issue where setting a property on a SwiftData object to a Codable struct with an optional String crashes the app.
For example, if I have the following model object and struct :
@Model
final class Item {
var timestamp: Date
…

Michael Berk
- 365
- 2
- 16
1
vote
2 answers
Does the #Predicate macro in Foundation/SwiftData support case-insensitive search?
I'm testing the new SwifData framework and working with the new #Predicate macro and realized that it doesn't support case-insensitive search. The macro does not work with the lowercased() method or the localizedLowercase property, so there seems to…

Mikiko Jane
- 463
- 6
- 13
1
vote
1 answer
How to check that a String has a specific prefix in a #Predicate macro?
I'm surprised to see that #Predicate doesn't support the basic hasPrefix(_:) function by turning it into the equivalent of BEGINSWITH in the old NSPredicate.
let predicate = #Predicate { $0.hasPrefix("some prefix") }
The hasPrefix(_:)…

parapote
- 433
- 2
- 8
1
vote
0 answers
Can I apply a macro to a struct in a Swift package?
I'm working on a new app which will use SwiftData. The models I want to persist are from an external package. Specifically, I want to persist Radical, Kanji, Vocabulary, and KanaVocabulary structs from the following…

UnicornsOnLSD
- 631
- 6
- 24
1
vote
0 answers
Parent/Child relationship - should parent be weak?
Normally, in a parent/child relationship, the parent property in the Child class would need to be weak. For example:
class Parent {
var children: [Child]
}
class Child {
weak var parent: Parent?
}
This would be done to avoid reference…

HangarRash
- 7,314
- 5
- 5
- 32
1
vote
1 answer
SwiftData error NSInvalidArgumentException when assigning relationship
I have the following code:
import SwiftUI
import SwiftData
struct ContentView: View {
@Environment(\.modelContext) private var modelContext
var body: some View {
VStack {
Image(systemName: "globe")
…

David Schilling
- 2,442
- 3
- 17
- 24
1
vote
1 answer
Can you create a 2D array with the new SwiftData macro?
I’ve been trying out SwiftData and I cannot seem to make a 2D Array. Is this a technical limitation of SwiftData? Or is there a work around?
When this code is run, and I press the button
struct ContentView: View {
@Query var tests: [Test]
…

Blue
- 13
- 3
1
vote
0 answers
SwiftData and correct setup for AppGroup
I have Xcode 15 Beta 2 and I am using SwiftData for my new app that is going to have also an extension and Apple Watch companion app.
At the moment I am configuring the iOS/iPadOS app as following:
My Shared Persistency Library
public let…

GrizzlyBear
- 1,098
- 1
- 13
- 27
1
vote
2 answers
View in SwiftUI does not get updated when model changes
I am inserting a new item into the modelContext (while on the single Deck) and the view is supposed to update automatically, but it does not.
It refreshes only when I navigate back from a single Deck view to the list of Decks view. I am not sure…

Damian Kociszewski
- 283
- 1
- 5
- 20