为什么GeoFire Query只在Swift中调用两次方法时填充数组?

时间:2016-11-19 03:34:02

标签: swift firebase firebase-realtime-database geofire

我有一个GeoFire查询,它只在方法keys被调用两次后用适当的getLocalUsers填充我的数组。如何才能通过1次呼叫才能实现此目的?这是我的代码......

@IBAction func getKeys(sender: AnyObject)
    {
        var allKeys = [String]()
        let geofireRef = self.ref.child("UserLocations")
        let geoFire = GeoFire(firebaseRef: geofireRef)
        let circleQuery = geoFire.queryAtLocation(self.location, withRadius: 200.6)
        circleQuery.observeEventType(.KeyEntered, withBlock: { (key: String!, location: CLLocation!) in
            var users = [String]()
            allKeys.append(key)
            for keys in allKeys
            {
                let user = keys
                users.append(user)
            }
            self.localUsers = users
            self.getLocalUsers()
        })
        self.getLocalUsers()
    }

1 个答案:

答案 0 :(得分:0)

我并不认为这是Firebase问题,但不确定。我认为最重要的是在keysallKeys的方法中创建一个数组,这样我就可以循环并正确地说出我的公共数组。以下是我如何使用它。 @FrankVanPuffelen请评论......

@IBAction getKeys()
{
     var allKeys = [String]()
        let geofireRef = self.ref.child("UserLocations")
        geofireRef.keepSynced(true)
        let geoFire = GeoFire(firebaseRef: geofireRef)
        let circleQuery = geoFire.queryAtLocation(self.location, withRadius: 200.6)
        circleQuery.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.friendsNearMeACTN()
        }) 
}