3

I have a user favorites class that's storing a user object and an event object, it get set when a user favorites an event.

I'd like to display the full list of a given user's favorites. So I query favorites class to get the event objects and then attempt to query the events table to match the event object from favorite with the actual event class to pull in the event name and ID. In ruby for example, assuming relations/associations are setup, I would just call:

favorite.event.name
favorite.event.id

What's the equivalent in Swift? Here's what I have so far, but something tells me I'm overcomplexifying it and I would hope there's simple methods available for retrieving data through relations.

let query = PFQuery(className: "Favorite")
    query.includeKey("eventId")
    query.whereKey("createdBy", equalTo:userId!)
    query.findObjectsInBackgroundWithBlock { (favorites:[PFObject]?, error:NSError?) -> Void in

        if (error == nil){
            print("Favorite Count: \(favorites!.count)")
            print("Favorite Event IDs: \(favorites!)")

            for favorite in favorites! {
                print("Favorite: \(favorite)")

                let eventNameQuery = PFQuery(className: "Event")
                eventNameQuery.whereKey("eventId", equalTo:favorite )
                eventNameQuery.findObjectsInBackgroundWithBlock { (events:[PFObject]?, error:NSError?) -> Void in

                    if (error == nil){
                        print(events!.count)
                        print(events!)

                        for event in events! {                            
                            self.favoriteListItems.append(event.objectId! as String)  
                            self.favoriteListIds.append(event.objectId! as String) 
                        }
                    }
                }
            }
            self.savedEventsListTableView.reloadData()
        } else {  
            print("error fetching objects") 
        } 
    }  
}

When I run this, I get zero results...when I know in parse core I have objects that match both as shown below:

Favorite Class: Favorite Class with event object and user object

Event Class: Event Class

4
  • so what's the problem that you are having ? Commented Dec 18, 2015 at 20:05
  • @Lamar - yeah, that would help. updated. thanks. Commented Dec 18, 2015 at 20:11
  • The picture just told us that... that part of your problem is that you are using the pointer of PFUser not objectId Commented Dec 18, 2015 at 20:14
  • like i mention in the comment below the first answer, the first query is returning results. it's the second query for events that's printing an empty array in the console. Commented Dec 18, 2015 at 20:51

1 Answer 1

1

First of all query for a pointer to a User object, not user's objectId.

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

Next in here you probably want to add name of event.

for event in events! {
 self.favoriteListItems.append(event["eventName"] as! String)
 self.favoriteListIds.append(event.objectId! as String)
}
Sign up to request clarification or add additional context in comments.

6 Comments

ok...made those edits, but still showing a blank array. the user part is working, because when i do print the favorites count and objects, i see them in the console. so the first query is working. it's the sub-query that is returning an empty array. so somehow querying the Event class with the favorite object is not pulling any related data. my guess is that it's this line that is wrong: eventNameQuery.whereKey("eventId", equalTo:favorite )
Try replacing eventNameQuery.whereKey("eventId", equalTo:favorite ) to eventNameQuery.whereKey("objectId", equalTo:(favorite["eventId"] as! PFObject).objectId) it's a bit rough, but let's see will it work or not
And another one: your table view is empty probably because you are calling self.savedEventsListTableView.reloadData() before fetching your Event-objects.
i appreciate all of your help, but yeah, i'm lost right now. i changed the eventNameQuery as you suggested and moved the reload data up a bunch. also added a print on the favoriteListItems to see what's in that array, but it's empty too. they don't make it easy to do such a common maneuver.
Actually I've spotted, you're using includeKey command, that means you don't need to fetch the Event object - it's received in the first query. So in for favorite in favorites! call self.favoriteListIds.append(favorite["eventId"] as! PFObject).objectId and self.favoriteListItems.append((favorite["eventId"] as! PFObject)["eventName"]as! String)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.