【发布时间】:2017-08-28 06:19:23
【问题描述】:
我使用 GeoFire 从我的 Firebase 数据库中获取基于位置的数据。我知道如果我在查询中设置较小的半径,那么我可以快速加载数据,但我的要求是我想要基于位置的短数据,所以最近的记录首先显示,依此类推。所以我在 GeoFire 查询中传递了当前位置和地球总半径,因为我想要所有数据。但我不知道如何使用 GeoFire 应用分页,所以将来当 Firebase 数据库中有更多记录可用时,我当前的实现肯定需要更多时间来加载。
以下是我用来获取基于位置的记录的代码片段。
let eartchRadiusInKms = 6371.0
let geoFire = GeoFire(firebaseRef: databaseRef.child("items_location"))
let center = CLLocation(latitude: (self.location?.coordinate.latitude)!, longitude: (self.location?.coordinate.longitude)!)
let circleQuery = geoFire?.query(at: center, withRadius: eartchRadiusInKms)
if CLLocationCoordinate2DIsValid(center.coordinate) {
circleQuery?.observeReady({
let myPostHandle : DatabaseHandle = circleQuery?.observe(.keyEntered, with: { (key: String?, location: CLLocation?) in
// Load the item for the key
let itemsRef = self.databaseRef.child("items").child(key!)
itemsRef.observeSingleEvent(of: .value, with: { (snapshot) in
// Manage Items data
})
})
})
}
那么 GeoFire 可以实现分页吗?或者我必须使用一些不同的机制,有人可以就这种情况给我建议吗?
【问题讨论】:
-
据我所知,GeoFire 默认不支持分页,因此您必须使用自己的机制。一种解决方案是在第一次呼叫时使用小半径,并在每次下次呼叫时增加它。我没有看到任何其他干净的解决方案。我们也想在我们的项目中使用 GeoFire,但由于它对我们的需求非常慢并且使用了太多的调用,我们最终编写了自己的 API。
标签: ios swift firebase geofire