ios - 在查询解析表之前获取位置查询

标签 ios xcode swift parse-platform location

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

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

我引用了这两篇文章...

Location Query in Parse TableView

Locations around the users with parse

但是,在将 PFGeoPoint.geoPointForCurrentLocationInBackground 放入 viewDidLoad 中(其中包含查询)或将其与查询放在单独的函数中时遇到了很多麻烦在它的内部(我有一个名为 queryForData 的单独函数,它允许拉动刷新工作,但我在添加 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
        }
    }
}
}

谢谢。

最佳答案

geoQuery 完成后,您不会加载表格。

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

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

currentLocation 应该是 PFGeoPoint 类型的 Controller 中的全局

关于ios - 在查询解析表之前获取位置查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31227997/

相关文章:

iphone - @try @catch 是否适用于 SIGSEGV/SEGV_ACCERR

ios - UICollectionViewCell 在滚动时消失

ios - 如何显示每个按钮点击不同的文本以制作讲故事的游戏

快速转义反斜杠不能按预期工作

ios - 当 snapshot.exists() 返回 false 时怎么办?

ios - 如何管理 Storyboard中的项目以添加展开/折叠 tableView 单元格的可能性

Swift:从 [Int: UIView]() 访问 UIView 子级

ios - 如何重新加载 UIAlertView 消息

c++ - 如何在 mac 上使用 cmake

ios - drawRect 不调整大小并跑出屏幕 3.5 英寸屏幕