Query objects from Realm and keep them in sequence


I want to query a list of words say [w1 w2 w3] from realm database, and I want also to keep the result in sequence. The Where Statement I wrote is :

word in {‘w1’,‘w2’,‘w3’}
The RLMResults contains these words, but they are not in the right sequence. Instead, they are sorted by characters.

One possible solution is, accessing the object in RLMResults and sorting them as I want. By doing this, I have to do the job in background thread, causing these objects can not be used in main thread.

To address the problem above, I have to make plain objects which are not managed by realm from the queried objects.

Is there any better solution?


I cant see why do you need to sort in a background thread, but if you really need to do so, then you can still make threadSafeReference from the result.

Unfortunately, you cant make sure how the result will be ordered, so its better to have your own sorting.


The RLMResults are lazily evaluated. If I want to do a custom sort, I have to access the real objects. If the word list is long, them the sorting process will block the main thread.


Can we have a bit of clarification? You can’t really query a list of words in Realm - you can query for objects that have a string property that matches a word. Or you can query an objects List property for a string.

Also, does your question mean you want to query Realm for certain objects and the order in which you list what you want to query for should match the order in which they are returned.

So for example: if you have a FruitClass with a a fruitName property. There are three Fruit objects stored in Realm with fruitNames of Pear, Banana, Apple. You query for Apple, Banana so the results should contain Fruit objects in that order (Apple, Banana)?

Can you include your Realm object class so we can see what it looks like and an example query?