Puneet Agarwal
Puneet Agarwal

Reputation: 43

Realm query which also filters custom object data nested in another RealmObject class

I am trying to create a query in Realm which filters out the custom object data which is itself is inside a RealmObject class. So for eg:

I have this class

public class Email extends RealmObject {

public static final String PROPERTY_SUBJECT = "subject";
public static final String PROPERTY_SENDER = "sender";
public static final String PROPERTY_BODY = "body";

  public String subject;
  public Sender sender;
  public Body body;
  public Date receivedDateTime;
}

public class Sender extends RealmObject {
  public EmailAddress emailAddress;
}

public class EmailAddress extends RealmObject {
  public String emailId;
  public String name;
}

public class Body extends RealmObject {
  public String content;
  public String contentType;
}

So I am trying to create a query which returns me all the results based on a user search to filter from "subject, emailAddress, name, bodyContent".

So I am trying it like this

Realm realm = Realm.getDefaultInstance();
RealmQuery<Email> query = realm.where(Email.class);

query.beginGroup();
query.contains(Email.PROPERTY_SUBJECT, queryString);
query.contains(Email.PROPERTY_SENDER, ?);
query.contains(Email.PROPERTY_BODY, ?);
query.endGroup();

RealmResults<Email> results = query.findAllSorted(Email.PROPERTY_RECEIVED_DATE_TIME, Sort.DESCENDING);

I am trying to figure it out that how I can put Sender and Body object in the query as they are custom objects.

Can anyone help me out as I am a newbie to Realm?

Thank you in advance.. :)

Upvotes: 0

Views: 1117

Answers (3)

EpicPandaForce
EpicPandaForce

Reputation: 81539

The answer is "link queries", although in your case you're just supposed to merge the objects into one.

public class Email extends RealmObject {
  private String subject;
  private String senderName;
  private String bodyContent;
  private String bodyContentType;
  private Date receivedDateTime;

  // getters, setters
}

Upvotes: 0

Puneet Agarwal
Puneet Agarwal

Reputation: 43

I figured out that what I was doing wrong in my Realm query. Basically, I was not using or() operator to combine multiple queries and that's the reason that I was not able to get the desired results.

Now I solved this problem on my own. Here is my solution:

    RealmQuery<Email> query = realm.where(Email.class);
    query.contains(Email.PROPERTY_SUBJECT, queryString, Case.INSENSITIVE).or()
        .contains(Email.PROPERTY_SENDER_NAME, queryString, Case.INSENSITIVE).or()
        .contains(Email.PROPERTY_SENDER_EMAIL, queryString, Case.INSENSITIVE).or()
        .contains(Email.PROPERTY_BODY_CONTENT, queryString, Case.INSENSITIVE);
    RealmResults<Email> searchResults = query.findAllSorted(Email.PROPERTY_RECEIVED_DATE_TIME, Sort.DESCENDING);

Where my Constants are something like this:

  public static final String PROPERTY_SUBJECT = "subject";
  public static final String PROPERTY_SENDER_NAME = "sender.emailAddress.name";
  public static final String PROPERTY_SENDER_EMAIL = "sender.emailAddress.address";
  public static final String PROPERTY_BODY_CONTENT = "body.content";

Also, you can see in the query that I have added Case.INSENSITIVEparameter as I wanted my search to be case insensitive.

Thank you for giving me suggestions above and trying helping me out. :) :)

Upvotes: 1

beeender
beeender

Reputation: 3565

You can take a look at doc about link queries.

first the beginGroup() and endGroup doesn't seem to be needed for your query.

try below:

Realm realm = Realm.getDefaultInstance();
RealmQuery<Email> query = realm.where(Email.class);

query.contains(Email.PROPERTY_SUBJECT, queryString);
query.contains("sender.emailAddress.name", "john");
query.contains("body.content", "some thing");

RealmResults<Email> results = query.findAllSorted(Email.PROPERTY_RECEIVED_DATE_TIME, Sort.DESCENDING);

Upvotes: 0

Related Questions