我的GeoFire查询带回了用户ID或keys
。我按顺序取回keys
,但我得到了几个序列。我怎样才能得到最后更新的序列?
@IBAction func friendsNearMeACTN(sender: AnyObject)
{
let geofireRef = self.ref.child("UserLocations")
let geoFire = GeoFire(firebaseRef: geofireRef)
let circleQuery = geoFire.queryAtLocation(self.location, withRadius: 20.6)
circleQuery.observeEventType(.KeyEntered, withBlock: { (key: String!, location: CLLocation!) in
self.localUsers.append(key)
self.getLocalUsers()
})
}
func getLocalUsers()
{
print(self.localUsers)
}
这是我从func getLocalUsers()
....
["WGueYzDjH4NW2vneHOyGmjf6PYB3"]
["WGueYzDjH4NW2vneHOyGmjf6PYB3", "Cg4pQj36ttNUuWNqtc16tIFmI0A2"]
["WGueYzDjH4NW2vneHOyGmjf6PYB3", "Cg4pQj36ttNUuWNqtc16tIFmI0A2", "N5pgqGEhW2f7PGGVmB3AQ8v1uPk2"]
我怎样才能简单地获取最后一个数组?
答案 0 :(得分:1)
答案是GeoFire查询是一个连续的异步调用,需要最终代码observeReadyWithBlock
才能仅为self.localUsers()
提供所收集的信息。这是一个例子......
let regionQuery = geoFire.queryWithRegion(self.region)
regionQuery.observeEventType(.KeyEntered, withBlock: { (key: String!, location: CLLocation!) in
var users = [String]()
users.append(key)
for keys in users
{
let user = keys
allKeys.append(user)
}
self.localUsers = allKeys
self.getLocalUsers()
})
regionQuery.observeReadyWithBlock({ () -> Void in
self.getLocalUsers()
})
答案 1 :(得分:0)
这里的问题是每次触发观察者块时都要调用getLocalUsers函数。你是为每一个结果调用它。每次执行观察器块时,都需要计算结果并将计数加1。当计数达到结果计数时,请调用getLocalUsers函数一次而不是3次。请尝试下面的代码。我没有测试过。
@IBAction func friendsNearMeACTN(sender: AnyObject){
var i = 0//The counter
let geofireRef = self.ref.child("UserLocations")
let geoFire = GeoFire(firebaseRef: geofireRef)
let circleQuery = geoFire.queryAtLocation(self.location, withRadius: 20.6)
circleQuery.observeEventType(.KeyEntered, withBlock: { (key: String!, location: CLLocation!) in
self.localUsers.append(key)
i += 1//Add one to i every time observer fires
if i == self.key.count {//if counter (i) is equal to the keys returned call getLocalUsers func once
self.getLocalUsers()
}
})
}
func getLocalUsers(){
print(self.localUsers)
}