在查询 Parse 之前获取位置查询 Table

Obtain Location Query before Querying the Parse Table

我在获取用户位置时遇到问题,然后 运行 PFQuery class。我遇到的问题是当我启动应用程序时,tableview 加载了一个空数据集。我知道查询工作正常,因为当我使用下拉方法刷新表视图时,它会在正确的位置加载正确的数据集。

所以我怀疑当我第一次启动应用程序时,查询在获取位置之前运行,但是在我手动刷新 tableview 之后,位置已经从位置管理器获取并加载了正确的数据集。

我参考了这两个帖子...

Location Query in Parse TableView

Locations around the users with parse

但是,在将 PFGeoPoint.geoPointForCurrentLocationInBackground 放入其中包含查询的 viewDidLoad 中或将其放置在包含查询的单独函数中时遇到了很多麻烦(我有一个名为 queryForData 的单独函数,它允许拉动刷新工作,但我在添加 PFGeoPoint.geoPointForCurrentLocationInBackground)...

时删除了该函数

无论我做什么,查询都无法成功运行,因为加载了 class 中的所有行,而不仅仅是适用于用户当前位置的数据行。

  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
}

然后我使用了我发布的另一个 Whosebug 问题中的代码...

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
        }
    }
}
}

谢谢。

您没有在 geoQuery 完成后加载 table。

此外,您不需要在 geoQuery 块中再次创建查询。您可以调用 Parse 的 loadObjects 方法,该方法将使用 queryForTable() 方法重新加载数据。

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

currentLocationPFGeoPoint

类型的控制器中应该是全局的