Docs Menu

Find Objects of a Type - Kotlin SDK

To find all objects of a type, open a realm and pass the type as a type parameter to realm.query():

// fetch all objects of a type as a flow, asynchronously
val frogsFlow: Flow<ResultsChange<Frog>> = realm.query<Frog>().asFlow()
val asyncCall: Deferred<Unit> = async {
frogsFlow.collect { results ->
when (results) {
// print out initial results
is InitialResults<Frog> -> {
for (frog in results.list) {
Log.v("Frog: $frog")
}
} else -> {
// do nothing on changes
}
}
}
}
// fetch all objects of a type as a results collection, synchronously
val frogs: RealmResults<Frog> = realm.query<Frog>().find()
for(frog in frogs) {
Log.v("Frog: $frog")
}
Note

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.

←  Find Object by Primary Key - Kotlin SDKSort Queries - Kotlin SDK →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.