I am suddenly getting an error when trying to get a specific object from Realm via it’s primary ObjectId key. This may be something I am overlooking or a change I was not aware of so wanted to put it out there.
Here’s the error
Invalid value ‘60574941858a6fbe9a7fff6e’ of type ‘Swift.__SharedStringStorage’ for ‘object id’ property ‘DogClass._id’.
And my DogClass
class DogClass: Object {
@objc dynamic var _id = ObjectId.generate()
@objc dynamic var name = ""
override static func primaryKey() -> String? {
return "_id"
}
}
and the code that causes the crash
let existingDog = realm.object(ofType: DogClass.self, forPrimaryKey: "60574941858a6fbe9a7fff6e")
I could have sworn this was working previously; the only change I made was updating to RealmSwift 10.7.2
This is a local only, non-sync’d Realm, macOS 11.2.3, Realm 10.7.2, XCode 12.4
Edit
I just noticed a similar question over in the MongoDB Realm forum
But is that solution the solution? Seems strange we’re forced to use StaticString as in
let ss: StaticString = "60574941858a6fbe9a7fff6e"
let theKey = ObjectId(ss)
let existingDog = realm.object(ofType: DogClass.self, forPrimaryKey: theKey)
ObjectId(string: "60574941858a6fbe9a7fff6e") can throw because the string you provide may not be of the required length or may contain character outside of the 0..e range.
This code should work:
do {
try let id = ObjectId(string: yourString)
let existingDog = realm.object(ofType: DogClass.self, forPrimaryKey: id)
} catch {
// Do something to react to the error
}
Was that a recent change? We have existing code in a project that has not been updated in a while where that was not required e.g. forPrimaryKey: some string.
Do you know with what SDK version that change was made?
I have it working in a current project but that should really be in the documentation so it’s clear this
let existingDog = realm.object(ofType: DogClass.self, forPrimaryKey: "xxxyyzzz")
is only applicable to non-sync’d realms that do NOT use ObjectID to generate primary keys.