androiddatabaserealmrealm-list

Realm recursive query - android


I have several instances in Realm: FeedItem that has field movie with type Movie. Movie contains field showTimes that is List of ShowTime objects.

-FeedItem:
  └ movie: Movie
      └ showTimes: List<ShowTime>
                           └ minPrice: Float

And I need to query all the FeedItem items that contains Movie that contains at least one Showtime with given value for minPrice field.

I could use something like this:

Realm.getDefaultInstance().where(FeedItem::class.java)
                                    .beginGroup()
                                    .equalTo(FeedItem::movie.name + "." + Movie::showTimes.name + "." + ShowTime::minPrice.name, 10f)
                                    .endGroup()
                                    .findAll()

But showTimes is the List and I need to check all the items.

Due to the existing architecture of project I can not query all the items FeedItem and then filter them. So I can't use simple solution like this one:

val feed = Realm.getDefaultInstance().where(FeedItem::class.java).findAll()

val filteredFeed = feed.filter { feedItem ->
    var b = false
    feedItem.movie.showTimes.forEach { showtime ->
        b = showtime.minPrice == 10f
    }
    b
}

And only way to solve my issue is to write one complex query to Realm. Tell me, please, how can I do that?


Solution

  • I found solution: How do I query RealmObject that have RealmList that contains specified value

    So, in my case:

    Realm.getDefaultInstance().where(FeedItem::class.java).beginGroup()
              .greaterThanOrEqualTo(FeedItem::movie.name
                            + "."
                            + Movie::showTimes.name
                            + "."
                            + ShowTime::minPrice.name,
                            10f)
               .endGroup()