Realm List Primary Keys


#1

Hello

I just want a fast way to list all the primary keys with Java / Kotlin. I’m a bit surprised there’s no obvious fast method like count().

I can do this, with any field:

realm.where().findAll().map { it.name }.toList()

but that seems like massive overkill as it will get all the records just to get the name field, which in this hypothetical example is the primary key.

There must be an easier way.

Any help?


#2

That is the fastest way. There is currently no API for only selecting a field or subset of fields. The reason for this is that Realm Objects are zero-copy anyway, so they don’t actually fetch the data until you request it.

In that sense the pattern you are using actually have a lot less overhead than you might think.