vladimir123 vladimir123 - 2 months ago 6
Android Question

How to use Realm's in method with Kotlin

I'm trying to do something like:

val barcodes = arrayOf("123", "456", "789")
realm.where(Product::class.java).in("barcode", barcodes).findAll()


However "in" is a Kotlin function and I can't access the in(String filedName, String[] values) method of RealmQuery object.

Currently I have a java class that does the job and returns the result, but I was wondering is there a more graceful workaround for this?

Answer

As stated in the Escaping for Java identifiers that are keywords in Kotlin:

Some of the Kotlin keywords are valid identifiers in Java: in, object, is, etc. If a Java library uses a Kotlin keyword for a method, you can still call the method escaping it with the backtick (`) character

For example:

realm.where(Product::class.java).`in`("barcode", barcodes).findAll()