1

I am getting a crash due to a java.lang.UnsupportedOperationException: Syntax error while executing such query:

public RealmResults<MyObject> getMyObjects(List<Integer> ids, boolean filter) {
    Realm realm = null;
    try {
        realm = Realm.getDefaultInstance();
        RealmQuery<MyObject> query = realm.where(MyObject.class);
        query = query.beginGroup();
        for (int i=0; i< ids.size(); i++) {
            query = query.equalTo("id", ids.get(i));
            if (i != ids.size() -1) {
                query = query.or();
            }
        }
        query = query.endGroup();
        if (filter) {
            query = query.equalTo("someBoolean", true);
        }
        return query.findAll();
    } finally {
        if (realm != null) realm.close();
    }
}

Removing the query = query.beginGroup(); and query = query.endGroup(); makes the syntax error go away but I'm not sure if the query will yield the same results.

Can somebody help me pointing out where the syntax error is and why it happens?

Marco Romano
  • 1,169
  • 7
  • 24
  • There is no further information about the error in the log, all I get is `java.lang.UnsupportedOperationException: Syntax error` – Marco Romano Aug 26 '16 at 09:17
  • You might wish to use https://realm.io/docs/java/1.2.0/api/io/realm/RealmQuery.html#in-java.lang.String-java.lang.Long:A- – geisshirt Aug 26 '16 at 09:39
  • What does `ids.size()` return? – geisshirt Aug 26 '16 at 09:40
  • Its the size of the input array, which must not be null. So that won't cause the crash. I use that if to append as many ORs I need except for the last element in the chain or i'll end up with a lone OR at the end of the query. – Marco Romano Aug 26 '16 at 10:17
  • Wow didn't know about the in() query method. It works indeed! But I still can't explain why the syntax error though. – Marco Romano Aug 26 '16 at 10:35
  • We introduced `in()` recently. It would be great if you can create a small example (or unit test) and a Github issue so we can see if there is a bug. – geisshirt Aug 26 '16 at 13:22
  • @geisshirt I've tried to reproduce the bug in a small project but I couldn't: the query reported above now executes fine. It could have been a PBKAC. – Marco Romano Aug 29 '16 at 08:07

1 Answers1

2

You should use the in() operator that was added in 1.2.0.

Previously I would have just told you to use this answer

Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428