ObjectId
We need a way for ids to not have to be unwrapped from $oid before they are assigned as an ObjectId.
Incoming data:
{"_id":{"$oid":"..."}}
Realm class:
class Item: Object {
@objc dynamic var _id = ObjectId.generate()
}
That's all that should be needed.
So the default ObjectId class needs:
enum CodingKeys: String, CodingKey {
case oid = "$oid"
}
then in init(from decoder:Decoder)
let container = try decoder.container(keyedBy: CodingKeys.self)
try super.init(string: try container.decode(String.self, forKey: .oid))
Do that when the .singleValueContainer() call fails.
That way both use cases are satisfied.
2
votes
Paul
shared this idea