gigashark
gigashark

Reputation: 29

Realm Query with 2 Columns and distinct

I would like to create a Realm Android query, but dont know how to do this. My realm Object is the following.

public class test extends RealmObject {
     @PrimaryKey 
     int id;
     String test1;
     String test2;
}

My query should search in test1 and test2. So if either test1 or test2 is equals to the filter String the RealmObject should be inserted into my list. It is possible that test1 and test2 both have the same value. If this is the case the Object should only appear once in my list and not twice.

My query which is not working:
 List<test> games = db.where(test.class)
                .contains("test1", charText, 
                 Case.INSENSITIVE).distinct().where(test.class)
                 contains("test2", charText, Case.INSENSITIVE)
                .distinct();

At last I want to sort the list after test1.

Upvotes: 0

Views: 1521

Answers (1)

jsa
jsa

Reputation: 1178

You could use RealmQuery::or to combine the conditions:

List<test> games = db.where(test.class)
                     .contains("test1", charText, Case.INSENSITIVE)
                     .or()
                     .contains("test2", charText, Case.INSENSITIVE)
                     .findAllSorted("test1");

You can find other examples of this type of query in the documentation

Even if the test1 and test2 contain the same value, the same record will not appear twice in the list so you do not need to add the distinct constraint on your query.

Upvotes: 2

Related Questions