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 PersistentModel
s:
@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 aPersistentModel
value - an overload that accepts a
KeyPath
with aDecodable
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
PersistentModel
s not supposed to beDecodable
? - If yes, what would be the preferred way (or a clean way) to decode a
PersistentModel
(e.g., from an API)?
Best regards, Jonas