Android Realm, query objects by child attribute Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Android Realm, query objects by child attribute without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m using Realm-Java for an Android application.

I need to query a list of MyObject, searching for the ones that contain a string in MyObject.SubObject_A.ListOfString.

Since Realm doesn’t support list of String, I’m now using this structure:

-MyObject
----SubObject_A
--------Attribute_A
--------Attribute_B
--------RealmList<RealmString>
----SubObject_B
----OtherStuff

With RealmString being

public class RealmString extends RealmObject {
  public static final String VALUE = "value";
  private String value;
}

How do I query for all MyObject that contain a given String inside MyObject.SubObject_A.RealmList<RealmString>?

Answer

You’re looking at link queries. You should be able to do something like this to get a RealmResults<MyObject>.

realm.where(MyObject.class).equalTo("subObject_A.stringList.value", "search string").findAll();

The idea is that you’re able to use a condition in equalTo that contains the path through the relationships separated by period.

We are here to answer your question about Android Realm, query objects by child attribute - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji