Skip to main content

Find Object by Primary Key

To find an object with a specific primary key value, open a realm and query the primary key field for the desired primary key value using realm.query(). Specify the object type as a type parameter passed to query():

// Search equality on the primary key field name
val frogs: Frog? = realm.query<Frog>("_id == $0", PRIMARY_KEY_VALUE).first().find()
find() is Synchronous

find() runs a synchronous query on the thread it is called from. As a result, avoid using find() on the UI thread or in logic that could delay the UI thread. Prefer asFlow() in time sensitive environments.