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

Juri Noga
  • 4,363
  • 7
  • 38
  • 51
jakeatwork
  • 487
  • 5
  • 17

1 Answers1

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)
}
Juri Noga
  • 4,363
  • 7
  • 38
  • 51
  • 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 )` – jakeatwork Dec 18 '15 at 20:50
  • 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 – Juri Noga Dec 18 '15 at 21:04
  • And another one: your table view is empty probably because you are calling `self.savedEventsListTableView.reloadData()` *before* fetching your `Event`-objects. – Juri Noga Dec 18 '15 at 21:07
  • 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. – jakeatwork Dec 18 '15 at 21:35
  • 1
    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) – Juri Noga Dec 18 '15 at 21:50
  • Brilliant! It worked...the name showed up first, but there was an issue with the IDs...played around a bit and `self.favoriteListIds.append((favorite["eventId"] as! PFObject).objectId! as String)` ended up being the right way to pull the IDs. Thank you so much for your help and persistence. – jakeatwork Dec 19 '15 at 00:02