Post

Replies

Boosts

Views

Activity

Reply to Swift 6 Concurrency errors with ModelActor, or Core Data actors
One solution could be to actually call save() and then have a manual rollback function that when called would delete the object with a given id. That way you could hopefully avoid creating strong dependencies between your services (actors). Of course if there are cases that are more complicated than the one in your code that returns a single id a solution like this could easily become quite complex so it depends on your use case if this is doable.
Mar ’25
Reply to multidatepicker and saving multiple dates via swiftdata
Use an Array instead of a Set in your model, use Date instead of DateComponents as the element type. If using an array gives you warnings in the console similar to CoreData: fault: Could not materialize Objective-C class named "Array" from declared attribute value type "Array" of attribute named ... then you can wrap the Date property in a Codable structure and use that as a type struct ToDoDate: Codable { let date: Date } In model: var dates: [ToDoDate] = []
Topic: Community SubTopic: Apple Developers Tags:
Feb ’25
Reply to SwiftData Predicates crashes when using Generic
There's a lot I don't understand about your question like what Reporter really is, how the ModelActor comes into play and what you mean with the last paragraph about the fetchAll method (did you write a "not" to much?) but one issue I see in your code is that you are passing PersistentModel objects from the detached Task to the UI which means you are passing them across actor boundaries and that is something you should not do since they are not conforming to Sendable. (Which swift version are you using and do you have any concurrency checking set?) This makes the whole solution look flawed and I would suggest that you instead fetch your objects on the MainActor using a @Query instead.
Dec ’24
Reply to SwiftData: Failed to decode a composite attribute
A migration is probably the best solution but if you don't want to do that you could use a custom decoding for the enum extension Kind { init(from decoder: any Decoder) throws { let container = try decoder.singleValueContainer() let rawValue = try container.decode(String.self) if let kind = Kind(rawValue: rawValue) { self = kind } else { let oldValue = rawValue.capitalized // <-- You might need to adjust this depending on how the rest of the enum looks. guard let kind = Kind(rawValue: oldValue) else { throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "Unknow kind: \(oldValue)")) } self = kind } } } Note that this will fix the value when read from storage but to actually store the new value, "Credit", each objects will need to be saved until it's persisted.
Nov ’24
Reply to SwiftData error "Thread 1: Fatal error: Composite Coder only supports Keyed Container"
If you create a Color.Resolved value in a playground and encodes it you see you get an array of doubles rather than a structure with RGB properties and the error clearly states that an array (keyless container) is not supported. So I guess the conclusion is that Color.Resolved isn't supported by SwiftData. My playground test code and output let color = Color.red var resolvedColor: Color.Resolved resolvedColor = color.resolve(in: EnvironmentValues()) let data = try! JSONEncoder().encode(resolvedColor) print(String(data: data, encoding: .utf8)!) [1,0.23137254,0.18823528,1]
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’24