Search code examples
androidrealmrealm-mobile-platformrealm-list

Filtering by nested RealmList item's attributes


I'm having issues writing a query to do what I want -- since I'm not sure it's possible from my knowledge base, I figured I'd ask a question here. I know of other ways to do this, but I'm trying to write a query that would result in me being able to use the query as a managed object, other solutions I've come up won't provide that. Here's the problem:

I have a SiteInfo and Level model, that holds the Site along with associated Levels:

open class SiteInfo(
    var orgId: Int = INVALID_ID,
    @PrimaryKey var id: Int = INVALID_ID,
    var site: Site = Site(),
    var levels: RealmList<Level> = RealmList()) : RealmObject()

open class Level(@PrimaryKey var id: Int = INVALID_ID,
                 var building: String = "",
                 var floor: String = "",
                 var site_id: Int = INVALID_ID,
                 var floorplan_url: String? = null) : RealmObject()

I'm trying to filter down sites, where list of levels isn't empty, and I've gotten that far:

realm.where(SiteInfo::class.java).not().isEmpty("levels").findAllAsync()

However, for a site to be valid, at least one of it's levels must have a non-null floorplan_url. I'm not sure there is query syntax that could support this. If anyone could let me know if this is possible, that'd be great -- otherwise to the workaround I go.


Solution

  • Have you tried

    realm.where(SiteInfo::class.java)
         .isNotEmpty("levels")
         .isNotNull("levels.floorplan_url")        
         .findAllAsync()
    

    ?