【问题标题】:Obtain Location Query before Querying the Parse Table查询解析表前获取位置查询
【发布时间】:2015-07-05 07:13:34
【问题描述】:

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

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

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

Location Query in Parse TableView

Locations around the users with parse

但是,将 PFGeoPoint.geoPointForCurrentLocationInBackground 放在 viewDidLoad 中并在其中放置查询或将其放置在带有查询的单独函数中时遇到了很多麻烦(我有一个名为 @987654326 的单独函数@ 允许拉动刷新工作,但我在添加 PFGeoPoint.geoPointForCurrentLocationInBackground) 时删除了该功能...

无论我做什么,查询都不会成功,因为类中的所有行都已加载,而不仅仅是应用于用户当前位置的数据行。

  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
}

然后我使用了我发布的其他 stackoverflow 问题中的这段代码......

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

谢谢。

【问题讨论】:

  • 您也可以发布您的代码吗?谢谢。
  • 嗨,我刚刚用我正在使用的代码更新了帖子。谢谢!

标签: ios xcode swift parse-platform location


【解决方案1】:

geoQuery 完成后,您没有加载表格。

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

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

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

【讨论】:

  • 成功了!谢谢!我有另一个快速的问题.. 当我在模拟器中运行它时,它不起作用。但是,当我在实际设备上运行它时,它确实有效。您知道造成这种差异的原因是什么吗?
猜你喜欢
  • 2011-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-15
  • 1970-01-01
  • 1970-01-01
  • 2020-11-06
  • 2019-04-05
相关资源
最近更新 更多