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
![](https://secure.gravatar.com/avatar/dab6682d75d37596d2e82a819d46f6be?size=40&default=https%3A%2F%2Fassets.uvcdn.com%2Fpkg%2Fadmin%2Ficons%2Fuser_70-6bcf9e08938533adb9bac95c3e487cb2a6d4a32f890ca6fdc82e3072e0ea0368.png)