Post

Replies

Boosts

Views

Activity

Reply to SwiftData ModelContext.insert crashes, why?
I would keep the ModelContainer as a stored property and create the ModelContext in each test or even better create both the container and context objects in each test. As for the issue with your initial code, the modelContext stored property is a reference to the modelContainer.mainContect but since the modelContainer object is local to the setUp method it will be released and the reference will no longer be valid when that method exits in your second solution the modelContext is a newly created object so it will remain in memory as long as the test is running.
Aug ’25
Reply to Mutating an array of model objects that is a child of a model object
An array is a value type in itself even if the elements are reference types. So if you do var myArray = project.subcategories ?? [] then any change to myArray will not affect project.subcategories but if you make a change to an object in myArray (a Subproject object?) then that object in the project.subcategories array will also be updated. So I guess the short answer is that if you want to add or remove objects to the project you need to work with project.subcategories I can't really help you with the rest because I don't really understand what you are saying in the paragraph after the last code snippet.
Sep ’25
Reply to Picker using SwiftData
The selection parameter for the picker should be a single item and not an array @State private var contractType: TypeOfContract = // some default type also the type of the value passed to .tag() must be the same type as the property above. So this issue is not so much about SwiftData but instead understanding how a picker works.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’25
Reply to SwiftData and discarding unsaved changes idea???
I would recommend using a struct instead that mirrors the properties in the model, no more work really than working with duplicate properties and probably much easier to handle state changes etc.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jul ’25
Reply to SwiftData Fatal error: Editors must register their identifiers before invoking operations on this store
Could it be that you create multiple instances of the ModelContainer?
Replies
Boosts
Views
Activity
Aug ’25
Reply to SwiftData Inheritance Query Specialized Model
You haven’t provided the details about CollectionItem which seems to be the class that causes the crash but I must say that it’s a very strange design you have where the superclass both has a children property of self and is being inherited. Do you really need such a complex solution?
Replies
Boosts
Views
Activity
Aug ’25
Reply to SwiftData Inheritance Query Specialized Model
Well a simplified version could be to only keep the current super class Item and use a Boolean (or an enum) property to define if it’s a collection or a link.
Replies
Boosts
Views
Activity
Aug ’25
Reply to SwiftData ModelContext.insert crashes, why?
I would keep the ModelContainer as a stored property and create the ModelContext in each test or even better create both the container and context objects in each test. As for the issue with your initial code, the modelContext stored property is a reference to the modelContainer.mainContect but since the modelContainer object is local to the setUp method it will be released and the reference will no longer be valid when that method exits in your second solution the modelContext is a newly created object so it will remain in memory as long as the test is running.
Replies
Boosts
Views
Activity
Aug ’25
Reply to SwiftData & CloudKit: Arrays of Codable Structs Causing NSKeyedUnarchiveFromData Error
Unrelated but times is a computed property so no need to mark it as Transient. You only need to do that for stored properties that shouldn't be persisted.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’25
Reply to Mutating an array of model objects that is a child of a model object
An array is a value type in itself even if the elements are reference types. So if you do var myArray = project.subcategories ?? [] then any change to myArray will not affect project.subcategories but if you make a change to an object in myArray (a Subproject object?) then that object in the project.subcategories array will also be updated. So I guess the short answer is that if you want to add or remove objects to the project you need to work with project.subcategories I can't really help you with the rest because I don't really understand what you are saying in the paragraph after the last code snippet.
Replies
Boosts
Views
Activity
Sep ’25
Reply to SwiftData ModelCoders.swift:1069 Unable to decode
MKMapPoint doesn’t conform to Codable so you can’t use it directly in your model. One solution is to store the coordinate properties in the model instead.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’25
Reply to Mutating an array of model objects that is a child of a model object
You don’t need to unwrap anything so just skip the if let self.project.subcategories?.removeAll { $0 == subcategory} calling a method on something that is nil in swift is perfectly fine
Replies
Boosts
Views
Activity
Sep ’25
Reply to Mutating an array of model objects that is a child of a model object
That’s a completely different issue and it’s not something you checked either in the code you have provided
Replies
Boosts
Views
Activity
Sep ’25
Reply to SwiftData: Unexpected backing data for snapshot creation
My guess is that a view is trying to access an instance of the object after it has been removed.
Replies
Boosts
Views
Activity
Oct ’25
Reply to Picker using SwiftData
The selection parameter for the picker should be a single item and not an array @State private var contractType: TypeOfContract = // some default type also the type of the value passed to .tag() must be the same type as the property above. So this issue is not so much about SwiftData but instead understanding how a picker works.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’25
Reply to Swiftui Picker with optional value selected in picker
You should read the answers to your previous question carefully and also see this question on stackoverflow.com on using optionals with a picker
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’25
Reply to Present User an error message when SwiftData save fails
Here is a good Apple tutorial on handling errors in a view.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’25
Reply to SwiftData: Crash when deleting from model, but only in prod
What do you mean with "more verbose way"? It seems to be the same command but one is wrapped in a generic function.
Replies
Boosts
Views
Activity
Oct ’25