I am absolutely loving Realm (0.92) in combination with Swift but have a question about reading an object from the database. My goal is to retrieve a single object with a known, unique ID (which also happens to be the primary key.
All the documentation appears to be oriented around queries for multiple objects which are then filtered. In this case I know the object ID and, since it is known to be unique, would like to retrieve it directly.
My current approach is as follows:
Realm().objects(Book).filter("id == %@", prevBook.nextID).first
public func filter(predicateFormat: String, _ args: CVarArgType...) -> Results<T>
EXC_BAD_ACCESS (code=1, address=0xedf)
objectForPrimaryKey() is what you're looking for:
Realm().objectForPrimaryKey(Book.self, key: prevBook.nextID). There's no simpler way than
Realm.objects(Book).first if you need to search for the object by something other than the primary key.