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.
Mar ’25
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
Mar ’25
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