Post

Replies

Boosts

Views

Activity

Reply to SwiftData duplicates values inside array on insert()
I didn't notice Feature was a many-to-many relationship, I thought the array was owned by the Car/CarData object, my bad. There is really no point in making your models conform to Identifiable and have a custom id property since they already conform to this protocol (unless you have some specific reason to use your own identifier value). I have no idea if this could help with the duplicate Feature objects issue but maybe it could be worth trying by removing the id property and use the default one instead.
3w
Reply to SwiftData duplicates values inside array on insert()
Your copy methods are in my opinion flawed, you create a new instance of the main object but you re-use the relationship object instead of making a new copy of that as well, so called deep copying. So in Car you should have func copy() -> Car { Car( name: "temporaryNewName", carData: carData.copy() //<-- New instance ) } And in CarData you need to do something similar but loop over the features array and copy each element. Perhaps unrelated but why do you need both Car and CarData, can't they be merged and personally I prefer to use the @Relationship property wrapper for my relationship properties to make the intention clearer
3w
Reply to Previews due to SwiftData Predicates in Xcode 16.3
A workaround is to store the Int value for MediaType in Media instead and use a computed property to switch between Int and MediaType @Model class Media { private var type: Int var mediaType: MediaType { get { MediaType(rawValue: type) } set { type = newValue.rawValue } } } //... } Which would give a predicate where we use Int values in the filtering static var predicate: (Predicate<Media>) { let image = MediaType.image.value let predicate = #Predicate<Media> { media in media.type == image } return predicate } Two other observations, you don't need a id property for your model, the @Model macro makes the type conform to PeristentModel that extends Identifiable so you already have that id property And secondly I would personally use an enum instead of a struct: enum MediaType: Int, Codable, Equatable, Hashable { case image = 0 case video case audio }
Mar ’25
Reply to Swift 6 Concurrency errors with ModelActor, or Core Data actors
I believe you need to rethink your design then, you can't have a bunch of different operations that needs to be saved together spread over different actors. It's just not possible. Maybe work with struct's instead so you can pass them between actors and have only one central actor that converts from struct objects to model objects and handles the storing and saving of objects in a single transaction.
Mar ’25
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