Post

Replies

Boosts

Views

Activity

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
Reply to [SwiftData] How to get the first 7 elements by using @Query?
If you create the query property using a FetchDescriptor then you can set a limit for the number of rows being fetched. The drawback of this solution is that it's not a one liner so you need to do it in the init @Query private var records:[Record] init() { var fetchDescriptor = FetchDescriptor<Record>(sortBy: [SortDescriptor(\Record.date, order: .reverse)]) fetchDescriptor.fetchLimit = 7 _categories = Query(fetchDescriptor) } If you for some reason don't want to do it in the init you could declare the fetch descriptor as a static variable and then pass it to the @Query declaration
Nov ’24
Reply to How to evolve from an attribute named throws?
If I understand you correctly the issue is that you can't use the word throws in your code as an attribute name but maybe you can work around that by using backticks around it. I know this works fine for variables and properties and in custom types so hopefully it works within a Core Data/SwiftData context as well. So something like this should work, var `throws`: String
Oct ’24
Reply to How to completely reset SwiftData?
Is this for a released app or only local in Xcode?
Replies
Boosts
Views
Activity
Nov ’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.
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Nov ’24
Reply to [SwiftData] How to get the first 7 elements by using @Query?
If you create the query property using a FetchDescriptor then you can set a limit for the number of rows being fetched. The drawback of this solution is that it's not a one liner so you need to do it in the init @Query private var records:[Record] init() { var fetchDescriptor = FetchDescriptor<Record>(sortBy: [SortDescriptor(\Record.date, order: .reverse)]) fetchDescriptor.fetchLimit = 7 _categories = Query(fetchDescriptor) } If you for some reason don't want to do it in the init you could declare the fetch descriptor as a static variable and then pass it to the @Query declaration
Replies
Boosts
Views
Activity
Nov ’24
Reply to SwifttData Record Update
ModelContext has a property autosaveEnabled that you can set to false and then handle validation and save logic in a button action or something
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Nov ’24
Reply to Type Eraser for Container
I am not sure exactly what you are asking but as far as the ModelContainer yo must supply all your model types, directly or indirectly, when you create the container object
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’24
Reply to SwiftData - Problems with Predicates and Relations
But Hashtag and HashtagFilter are two different models so why should their persistentModelID values be equal? Your variable naming confused me.
Replies
Boosts
Views
Activity
Oct ’24
Reply to SwiftData thread-safety: passing models between threads
You should pass the id (persistentModelID), as you do in your code above, across actor boundaries and never the object itself.
Replies
Boosts
Views
Activity
Oct ’24
Reply to SwiftData - Problems with Predicates and Relations
where does the content of filterHashtags come from?
Replies
Boosts
Views
Activity
Oct ’24
Reply to How to evolve from an attribute named throws?
If I understand you correctly the issue is that you can't use the word throws in your code as an attribute name but maybe you can work around that by using backticks around it. I know this works fine for variables and properties and in custom types so hopefully it works within a Core Data/SwiftData context as well. So something like this should work, var `throws`: String
Replies
Boosts
Views
Activity
Oct ’24
Reply to Issues with SwiftData One-to-Many Relationships
https://stackoverflow.com/a/79090346/9223839
Replies
Boosts
Views
Activity
Oct ’24
Reply to SwiftData relationship crash on 17.x
I only use the @Relationship macro at one end of the relationship to avoid any issues. Maybe something has changed in the latest version that allows us to use it at both properties but in your case there is really no reason to do so.
Replies
Boosts
Views
Activity
Oct ’24
Reply to Thread Error with @Model Class
Try to remove the database file so that SwiftData can generate a new one.
Replies
Boosts
Views
Activity
Oct ’24
Reply to Swiftdata forcing @Model rename
Looks like you need to perform a migration, see for example https://developer.apple.com/wwdc23/10195
Replies
Boosts
Views
Activity
Oct ’24
Reply to After upgrading to XCode 16 app stopped working
Your model looks a bit strange to me. Why do you have a many to many relationship between the two models and why do you have an id property of type Float? Is it correct to assume that the float is the actual value (size) here and if so why is then the value also the identifier and why does each value need to be unique?
Replies
Boosts
Views
Activity
Oct ’24