【发布时间】:2016-09-14 06:04:51
【问题描述】:
我正在尝试使用 GeoFire 来检索并根据半径(例如,10 公里距离内的所有内容)。
为了清楚起见,我将详细信息和位置分别保存在数据库中,因此详细信息的 id 与位置的 id 相同。在保存数据时,我使用:
var details = ["a":"a", "b":"b", etc]
let checkpointRef = eventRef.childByAutoId()
checkpointRef.setValue(details)
let checkpointId = checkpointRef.key
let geofireRef = ref.childByAppendingPath("checkpointLocations")
let geoFire = GeoFire(firebaseRef: geofireRef)
geoFire.setLocation(CLLocation(latitude: usersLatitude, longitude: userLongitude), forKey: checkpointId) {...
到这里为止,数据库中的一切似乎都很好。
在检索数据时,从核心位置获取用户的纬度和经度后,我正在尝试:
func retrieve() {
let geofireRef = Firebase(url: self.baseurl + "/checkpointLocations")
let geoFire = GeoFire(firebaseRef: geofireRef)
let center = CLLocation(latitude: usersLatitude, longitude: usersLongitude)
var circleQuery = geoFire.queryAtLocation(center, withRadius: 10)
circleQuery.observeEventType(.KeyEntered, withBlock: { (key: String!, location: CLLocation!) in
print("Key '\(key)' entered the search area and is at location '\(location)'")
})
observeEventType() 出现了第一个问题。我不想连续观察事件。相反,就像经典的 TableView 一样,我想检索一次数据并显示,并使用“拉动刷新”功能重新检索数据。我的第一个问题是,GeoFire 是否有任何功能可以作为 Firebase 的 observeSingleEventOfType 并检索一次数据?
其次,这个函数会打印几次这个警告:
[Firebase] 使用未指定的索引。考虑将 /checkpointLocations 处的 ".indexOn": "g" 添加到您的安全规则中以获得更好的性能
..但不记录任何其他内容。我不认为它进入函数内部,因为它不打印 print("Key.. 部分。也不是print("A")。
我认为这可能是由“Firebase 规则”引起的。在 SO 上使用这个answer,我尝试添加这个:
"rules": {
"checkpointLocations": {
"$key": {
".read": true,
".write": true,
"geofire": {
".indexOn": "g"
}
}
}
}
还有这个:
"rules": {
"checkpointLocations": {
".read": true,
".write": true,
"geofire": {
".indexOn": "g"
}
}
}
但两者都不起作用。
更新:
使用@Gregg 的回答,我修复了警告,但是它仍然没有进入闭包内。
【问题讨论】:
标签: ios swift firebase core-location geofire