RJH
RJH

Reputation: 358

Parse and swift relational query

I have the following Parse backend for a fantasy football style app:

Class: _Users
standard columns

Class: Leagues
objectID
leagueName (String)
chairman (pointer to _User who created the league)

Class: LeagueMembers
objectID
memberID (pointer to _User)
leagueID (pointer to _Leagues objectId)

I want to display a list of leagues that a given user is a member of (they can be member of multiple leagues). What swift code can I use to perform a query which searches "LeagueMembers" for all records that the currentUser is a member of, and with that result, obtain the leagueID (from "LeagueMembers") and then fetch the leagueName from "Leagues" based on the leagueID? I have tried code similar to:

var query = PFQuery(className: "LeagueMembers")

    query.includeKey("memberID")

    query.whereKey("memberID", equalTo: PFUser.currentUser()!)

    query.findObjectsInBackgroundWithBlock{

        (objects: [AnyObject]?, error: NSError?) -> Void in

...

but I would then need to conduct another query based on the results to fetch the League Name from "Leagues"

Upvotes: 0

Views: 394

Answers (1)

cakl
cakl

Reputation: 400

You can eager load the foreign key "Leagues" like you have eager loaded the Users FK.

query.includeKey("leagueID")

Then you should be able to access all properties of League pointer.

Upvotes: 1

Related Questions