Danny February 2016

Parse.com Swift Multiple Class Query

I'm using Parse.com to store my user information but struggling to figure the following out.

With Parse.com I have three classes:

1) User

2) Place

3) SavedPlace

SavedPlace is a class which associates the User with a Place which the user saved. For each place in SavedPlace, there is the User and Place pointer as well as some additional notes created by the user.

In my app, I want to retrieve the list of the user's saved places into a TableView with the Place info (i.e. name, location, image) and the notes.

My current method is:

1) Query SavedPlace for all the saved places and return an array of Place objectIDs

2) Lookup each objectID in Place class and return the Place information.

Is there a more efficient way where I can query Parse once for all of the user's saved places and return all the relevant information from the Place class?

Thanks

Answers


EmilioPelaez February 2016

There is.

PFQuery has a method called includeKey:, which allows you to add objects that will be returned with your query, in your case, you want to get the users and places related to the SavedPlace instances, right? So you do this:

let query = SavedPlace.query()
query.includeKey("place")
query.includeKey("user")
query.findObjects...

The result will be a list of SavedPlace objects with a pointer for user and place, where those objects have already been fetched, so all their fields (that aren't pointers) will be available to you.

You can even use dot notation with that method, so if you have a pointer in your Place class called Country and you want to fetch it as well, you can add query.includeKey("place.country") and it will be included in the query.

The best thing about this is that it will count as a single call.

Here's more information about includeKey

http://blog.parse.com/announcements/queries-for-relational-data/

Post Status

Asked in February 2016
Viewed 1,310 times
Voted 13
Answered 1 times

Search




Leave an answer