Hello,
I have just tried playing around with the new Swift Data framework and noticed something.
Assume the following many-to-many relationship on two PersistentModels:
@Model
class Media {
@Relationship(.nullify, inverse: \Tag.medias)
var tags: [Tag]
}
@Model
class Tag {
var medias: [Media]
}
This compiles without problems. If we now make Media conform to Codable (or just Decodable), we get a compiler error: "Ambiguous use of 'getValue(for:)'".
When expanding the @Model and the then revealed @PersistedProperty macro, we see that the error is in the getter of Tag.medias, where we call self.getValue(for: \.medias). It seems the compiler knows multiple overloads of this function, including:
an overload that accepts a KeyPath with a PersistentModel value
an overload that accepts a KeyPath with a Decodable value
Since Media conforms to both protocols, the compiler understandably does not know which overload to use.
So to my questions:
Is this intended behavior? So are PersistentModels not supposed to be Decodable?
If yes, what would be the preferred way (or a clean way) to decode a PersistentModel (e.g., from an API)?
Best regards,
Jonas