1

Okay so I have a view where I need to show all the posts by a business near the user's location. The way my database is set up is such that there is a "BusinessPost" class that contains, among other things, a pointer to my "Businesses" class, so that each post has an owner.

When I'm trying to write the query for my view, I can't seem to figure out how to only get the posts from the user's current location. I can get all the posts from every business, or I can get every business in the user's location, but I can't figure out a way to combine the two in order to get just the posts that came from businesses near where the user is.

My thinking was to first get all the businesses near the user's location, and then make a second query using the "inQuery" argument, to find all the posts that had a pointer reference to a business near the user's location that was gotten in the first query.

However, this won't work, and I've been stuck on this for a couple of days. Does anyone have any suggestions? Help would be greatly appreciated.

  getPostsFromLocatedBusinesses.whereKey("Businesses", matchesKey: "objectId", inQuery: getBusinessesInLocation)

EDIT 1: Okay so i rewrote it according to what you suggested and came up with the following, but it still doesn't produce any results:

override func queryForTable() -> PFQuery! {
    var getPosts = PFQuery(className: self.parseClassName)
    var getBusinesses = PFQuery(className: "Businesses")

    if locationUpdated == false {
        return nil
    }
    else {
            getBusinesses.whereKey("City", equalTo: self.city)
            getBusinesses.whereKey("State", equalTo: self.state)
            getPosts.whereKey("Business", matchesQuery: getBusinesses)
            getPosts.includeKey("Business")


    self.tableView.hidden = false
    self.tableView.reloadData()
    return getPosts
    }
}
4
  • 1
    What's the name of the pointer column on BusinessPost that points to Business class? The inner query should be the query you have working on businesses near the user. Then the BusinessPost query should just be whereKey: (the name of your pointer column on BusinessPost) matchesQuery: the inner query. Commented Feb 23, 2015 at 17:45
  • @danh I rewrote the query and edited my original post. Commented Feb 23, 2015 at 19:50
  • Hmm. It looks like it should work. If you've double checked all of your key names, the parse class names, etc., and that self.city and self.state are as expected, and that locationUpdated is true, etc... and you're sure that there are rows in the data that satisfy those criteria, then I'm stumped. Commented Feb 23, 2015 at 21:04
  • 1
    Glad to hear. Please post an answer that will help others. Commented Feb 24, 2015 at 15:54

1 Answer 1

2

In order to query based on two separate Parse classes, where one part of the query is a pointer to another class, the best way to do so is to have an outer query on the class which contains the pointer, and then an inner query that finds whatever supplementary objects you're looking for. In my case, this meant finding businesses that were in the user's location, and then only showing Business Posts near the user's location, where in the "BusinessPost" class, the key "Business" is a pointer to my "Business" class, that contains other information about the business that the UI needed. The query would look as follows:

override func queryForTable() -> PFQuery! {
var getPosts = PFQuery(className: self.parseClassName)
var getBusinesses = PFQuery(className: "Businesses")

if locationUpdated == false {
    return nil
}
else {
        getBusinesses.whereKey("City", equalTo: self.city)
        getBusinesses.whereKey("State", equalTo: self.state)
        getPosts.whereKey("Business", matchesQuery: getBusinesses)
        getPosts.includeKey("Business")


self.tableView.hidden = false
self.tableView.reloadData()
return getPosts
}

Hope this can help others that are looking to accomplish a similar query with Parse and Swift.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for sharing this. I had built some funky arrays that took me a few days to nut out only to use what this did in 3 lines.

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.