nbokmans
nbokmans

Reputation: 5747

Realm query for all entities that have a field with a certain value

Sorry if my title is vague, I didn't know how else to describe it.

I am making an Android application in which users can browse a list of events at a festival, click on them for a more detailed overview of the event and it's performance times.

Here's my Java model:

public class FestivalEntity extends RealmObject {
    @PrimaryKey
    public String code;
    public String description;
    public String title;
    public int minimumAge;
    public String squareImage;
    public String landscapeImage;
    public RealmList<PerformanceEntity> performances;
    public Date firstPerformance;
}

public class PerformanceEntity extends RealmObject {
    public Date start;
    public Date end;
    public double price;
    public boolean favorite;
}

My question is: how do I make a query that finds all FestivalEntitys that have a PerformanceEntity that is favorite? I can't select an individual PerformanceEntity because there is no primary key for the PerformanceEntity table.

What I am looking for is something similar to this (invalid) query:

//Should return a list of FestivalEntity's that contain at least one performance that is favorite
RealmResults<FestivalEntity> results = realm.where(FestivalEntity.class).any("performances.favorite", true).findAll(); 

Upvotes: 2

Views: 1076

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81539

Actually, I think your use-case is the following query :

RealmResults<FestivalEntity> results = realm.where(FestivalEntity.class)
     .equalTo("performances.favorite", true)
     .findAll(); 

Please check if I'm right though, link queries always confuse me.

Upvotes: 3

Related Questions