0

I was having issues with obtaining the location of the user and then running a PFQuery for the class. The issue I am having is when I launch the app the tableview loads an empty data set. I know the query is working fine because when I refresh the tableview with the pull down method it loads the right data set with the right location.

So I suspect when I first launch the application the query runs before obtaining the location, but then after I manually refresh the tableview the location has been obtained from the location manager and the right data set is loaded.

I referenced these two posts...

Location Query in Parse TableView

Locations around the users with parse

But, was having a lot of trouble when placing the PFGeoPoint.geoPointForCurrentLocationInBackground in the viewDidLoad with the query inside of it or placing it in a separate function with the query inside of it (I have a separate function called queryForData which allows the pull to refresh to work, but I remove that function when adding the PFGeoPoint.geoPointForCurrentLocationInBackground)...

Whichever I do, the Query does not work successfully as the all the rows in the class are loaded instead of just the data rows that apply to the user's current location.

  override func queryForTable() -> PFQuery! {

    let query = PFQuery(className: "Posts")
    if let userLocation = currLocation {
        query.whereKey("location", nearGeoPoint: PFGeoPoint(latitude: userLocation.latitude, longitude: userLocation.longitude), withinKilometers: 5)
        query.orderByDescending("createdAt")
    } else {

    }
    return query
}

And then I was using this code from that other stackoverflow question I posted...

PFGeoPoint.geoPointForCurrentLocationInBackground {
(point:PFGeoPoint!, error:NSError!) -> Void in
if error == nil {
    //Point contains the user's current point

    //Get a max of 100 of the restaurants that are within 5km,
    //ordered from nearest to furthest
    var query = PFQuery(className: "Posts")
    query.limit = 100
    query.whereKey("location", nearGeoPoint: point, withinKilometers: 5.0)
    query.findObjectsInBackgroundWithBlock{
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if (error == nil) {
            //objects contains the restaurants
        }
    }
}
}

Thank you.

Community
  • 1
  • 1
R S
  • 73
  • 8

1 Answers1

0

You are not loading the table after the geoQuery completes.

Also, you don't need to create a query again inside the geoQuery block. You can call Parse's loadObjects method, which would reload data using the queryForTable() method.

PFGeoPoint.geoPointForCurrentLocationInBackground {
(point:PFGeoPoint!, error:NSError!) -> Void in
    if error == nil {
        currentLocation = point
        self. loadObjects()
    }
}

currentLocation should be global within the controller of type PFGeoPoint

kRiZ
  • 2,320
  • 4
  • 28
  • 39
  • That worked! Thanks! I had another quick question.. When I run this in the simulator, it doesn't work. But, when I run it on an actual device, it does work. Do you know what accounts for this discrepancy? – R S Jul 07 '15 at 02:32