【问题标题】:How do I properly use a stored search radius with GeoFire?如何在 GeoFire 中正确使用存储的搜索半径?
【发布时间】:2016-07-30 03:41:32
【问题描述】:

我正在使用 GeoFire 搜索给定的半径,用户可以在我的应用中设置并存储在 FireBase 上。当页面加载时,在运行 GeoFire 查询之前,我从 Firebase 获取半径。但是,当我运行下面的代码时,我收到以下错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“精度必须小于 23!”

通过玩弄一些策略性放置的打印语句,看起来 searchRadius 在 GeoFire 查询运行时返回为 0,这让我怀疑异步加载正在发挥作用。

我的问题是,我是否会因为我的 searchRadius 为 0 而收到此错误,如果是这样,我如何确保抓取我的用户搜索半径的 FireBase 块在我的 GeoFire 查询之前运行?

    self.ref.childByAppendingPath("users/\(self.ref.authData.uid)/searchRadius").observeEventType(.Value, withBlock: { snapshot in
        self.searchRadius = snapshot.value as! Double
    })

    let center = CLLocation(latitude: 37.331469, longitude: -122.029825)
    let circleQuery = geoFire.queryAtLocation(center, withRadius: self.searchRadius)



    circleQuery.observeEventType(GFEventTypeKeyEntered, withBlock: { (key: String!, location: CLLocation!) in

        //Do code for each result returned

    }) //End GeoFire query

【问题讨论】:

    标签: ios swift asynchronous firebase geofire


    【解决方案1】:

    是的,这是一个异步加载问题。即使 Firebase 已经缓存了 searchRadius 的值,值回调也不会内联执行。该线程将在值更新之前继续设置 GeoFire 查询。

    基于此代码 sn-p,您可以通过将查询代码移动到回调内部来确保在运行查询之前设置半径。您可能还想观察单个事件,以便在 searchRadius 的值发生变化时不会再次运行查询。

    self.ref.childByAppendingPath("users/\(self.ref.authData.uid)/searchRadius").observeSingleEventOfType(.Value, withBlock: { snapshot in
        self.searchRadius = snapshot.value as! Double
        let center = CLLocation(latitude: 37.331469, longitude: -122.029825)
        let circleQuery = geoFire.queryAtLocation(center, withRadius: self.searchRadius)
        circleQuery.observeEventType(GFEventTypeKeyEntered, withBlock: { (key: String!, location: CLLocation!) in
            //Do code for each result returned
        }) //End GeoFire query
    }) // End observeSingleEventOfType
    

    【讨论】:

    • 谢谢!非常感谢您的解释。
    猜你喜欢
    • 2014-09-27
    • 2019-02-01
    • 2013-02-19
    • 1970-01-01
    • 2016-09-06
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多