Skip to main content
The 2024 Developer Survey results are live! See the results

Questions tagged [swiftdata]

SwiftData is a Swift-based Apple framework for managed persistence. It combines CoreData persistence technology with Swift’s modern concurrency features.

22 votes
1 answer
3k views

SwiftData - How to observe changes to the database outside of SwiftUI?

I have a simple example. I fetch all Trip objects from my database: import SwiftData @Model class Trip { var name: String } func fetch() { let container = ModelContainer(for: Trip.self) ...
SwiftedMind's user avatar
  • 4,131
19 votes
3 answers
6k views

How to persist custom enum with SwiftData?

I want to save a custom enum to my SwiftData model. I can get it to work that it shows and even updates the value in the UI. Though, the change of the enum var/entity isn’t persisted when relaunching ...
alexkaessner's user avatar
  • 2,616
17 votes
3 answers
6k views

SwiftData list bindings with @Query

Since Swift 5.5 we could create SwiftUI lists with bindings like this (e.g. see this answer): class Item { // ... var isOn: Bool } struct ContentView: View { @State private var items: [...
de.'s user avatar
  • 8,166
16 votes
8 answers
8k views

How to resolve SwiftData error "Type '*' does not conform to protocol 'PersistentModel'"

Working through the betas of SwiftData and trying to stand up a PersistentContainer. I set up a modelContainer View modifier on my ContentView and conformed my class to @Model as described in various ...
Brianna Doubt's user avatar
16 votes
2 answers
2k views

Crash when accessing relationship property of SwiftData model

I have two one-to-many models and I'm getting an unexplained crash when trying to access the relationship properties of the models. The reason for the error is as follows: Thread 1: EXC_BREAKPOINT (...
ikunhub's user avatar
  • 253
16 votes
6 answers
3k views

SwiftData IOS 17 Array in random order?

Why is the order of my array random when i use the @Model macro. class TestModel { var name: String? var array: \[TestModel2\] init(name: String = "") { self.name = name ...
Florian Liegl's user avatar
15 votes
2 answers
6k views

SwiftData @Query with #Predicate on Relationship Model

XCode 15 beta 6. Just want to do a very simple Query Predicate where the relationship model matches: @Query( filter: #Predicate<Piece> { $0.artist == selectedArtist }, sort: [...
William T.'s user avatar
15 votes
4 answers
9k views

SwiftData query with dynamic properties in a View

I'm trying to figure out how to make a SwiftUI view that displays data from SwiftData using a query that includes variables passed into the view. I'm guessing that I won't be able to use the @Query ...
Mike Bedar's user avatar
14 votes
2 answers
2k views

How can we use custom structs/enums with the Model and Predicate macros?

Swift 5.9 and the new SwiftData framework introduce the @Model and #Predicate macros. We can now use custom enums and structs in our our models, like so: @Model final class Item { var name: Name ...
parapote's user avatar
  • 862
12 votes
1 answer
4k views

Using SwiftData outside of SwiftUI

I want to create a class for handling some data at regular intervals. I've not used CoreData. Outside of a SwiftUI view, how do I get the model container and context so I can retrieve and add objects? ...
Aaron Bratcher's user avatar
12 votes
1 answer
533 views

Crash when sorting SwiftData Query with a SortDescriptor made from a KeyPath with an optional relationship in the chain

I have the following models: @Model class Game { var name: String var firstReleasedOn: Date? @Relationship(deleteRule: .cascade, inverse: \QueueEntry.game) var queueEntry: QueueEntry? ...
Ben Walker's user avatar
12 votes
0 answers
723 views

Sharing SwiftData with iCloud users

has anyone figured out how to use swiftdata in SwiftUI with CloudKit to share data among other users and set permissions? Thank you! I checked apple’s documentation along with the internet in general. ...
Adam Collin Mitchell's user avatar
9 votes
3 answers
3k views

Combining Predicate in SwiftData

I'm trying to combine multiple Predicates of the Type with and / or. Previously with CoreData and NSPredicate I'd just do this: let predicate = NSPredicate(value: true) let predicate2 = NSPredicate(...
nOk's user avatar
  • 3,319
9 votes
2 answers
3k views

SwiftData passing @Model objects as parameters in SwiftUI

I am trying to work with multiple views in a SwiftData based SwiftUI project. However, when I create a parameter for a SwiftData @Model object in another view, when I create a sample of that object in ...
Syd Polk's user avatar
  • 123
9 votes
1 answer
1k views

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? ...
PerroVerde's user avatar

15 30 50 per page
1
2 3 4 5
38