Querying a List from List - and getting back Realm Results


#1

I have Event, Team, and Attempt objects.

Event contains a List<Team>()
and
Team contains a List<Attempt>()

How can I query for ALL Attempts for a particular Event?

I’m struggling to find a way to go from Event->Team->Attempt(s)

An Event can have multiple teams, and a Team can have multiple Attempts.

I’m able to do it by creating an array of [Attempt], but I’d really like to get a Result or List instead.

Can anyone help me understand if this is even possible? If I can get this done, then I’d like to be able to sort the results - but one step at a time.

Thanks!


#2

Did you know that list properties create a bidirectional link? To query over lists in the reverse direction you can assign names for these relationships as “linkinObject” properties. There’s some more information here: https://realm.io/docs/javascript/latest/#inverse-relationships

Once you have the linkingObjects, the query should be something like: let attempts = realm.objects('Attempt').filtered('byTeam.forEvent.name == "someEventName"');

The above example query makes some big assumptions, but that’s because I don’t have very many details about your models. If you need more specific help with the query, I’m happy to help if you can provide the schema of the three classes in question.