Geofire / Firebase函数在swift

时间:2018-06-06 14:19:26

标签: swift firebase geofire

我有以下功能,可以在嵌套设置中使用Geofire然后使用Firebase查询来获取用户列表。

我首先执行Geofire查询以获取key,然后执行Firebase查询并创建User对象,该key对象与追加User的{​​{1}}匹配然后通过handler传回的数组。

然后在User中使用array CollectionView,问题是在CollectionView显示时它只显示一个User个对象

我在调试代码中放置了一些print()行,并且发现每次附加一个项目时函数都通过handler传递数据,因此按时间计算数组的计数达到显示VCCollectionView的{​​{1}}。

将以下行移至1括号外,传递空for loop

如何更新我的功能,以便array只调用一次并传递完整的handler

array

这是功能:

handler(self.shuffleArray(array: filteredUsers) as! [User], true)

控制台:

    //Fetches all users currently at a Venue location of radius 50 metres
func getUsersAtVenue(forVenueLocation venueLocation: CLLocation, forUid uid: String, handler: @escaping (_ users: [User], _ success: Bool) -> ()){

    print("uid: \(uid)")

    var users = [User]()

    guard let currentLocation = LocationSingleton.sharedInstance.lastLocation else { return}//get current user's (device) location

    let distanceApart = round(10 * (venueLocation.distance(from: currentLocation) / 1000)) / 10 //get distance between currentLocation and venueLocation and convert from Mts to Kms rounding to 2 decimals

    if distanceApart <= 50 { //if distance apart if within 50kms then proceed

        let query = self.GEOFIRE_USERS_LOC.query(at: currentLocation, withRadius: 50)//radius in Kms

        query.observe(.keyEntered) { (key: String!, userLocation: CLLocation!) in

            print(key)

            self.REF_USERS.observeSingleEvent(of: .value, with: { (snapshot) in

                guard let usersSnapshot = snapshot.children.allObjects as? [DataSnapshot] else { return }

                for user in usersSnapshot{

                    let discoverable = user.childSnapshot(forPath: "discoverable").value as! Bool

                    if user.key == key && discoverable == true {

                        let uid = user.key
                        let name = user.childSnapshot(forPath: "name").value as! String
                        let email = user.childSnapshot(forPath: "email").value as! String
                        let profilePictureURL = user.childSnapshot(forPath: "profilePictureURL").value as! String

                        let birthday = user.childSnapshot(forPath: "birthday").value as! String
                        let firstName = user.childSnapshot(forPath: "firstName").value as! String
                        let lastName = user.childSnapshot(forPath: "lastName").value as! String
                        let gender = user.childSnapshot(forPath: "gender").value as! String
                        let discoverable = user.childSnapshot(forPath: "discoverable").value as! Bool
                        let online = user.childSnapshot(forPath: "online").value as! Bool

                        let dictionary: [String : Any] = ["uid": uid, "name": name, "email": email, "profilePictureURL": profilePictureURL, "birthday": birthday, "firstName": firstName, "lastName": lastName, "gender": gender, "discoverable": discoverable, "online": online]

                        let user = User(uid: uid, dictionary: dictionary)

                        users.append(user)

                    }//end if

                }//end for

                //filter out current user from array
                let filteredUsers = users.filter({ (user: User) -> Bool in return !user.uid.contains(uid) })

                print("filteredUsers count: \(filteredUsers.count)")

                //handler passing a shuffled version of the array
                handler(self.shuffleArray(array: filteredUsers) as! [User], true)

            })//end FIR snapshot call

        }//end geoquery


    } else {//if distanace apart is NOT within 50kms then do this
        print("You're too far apart.")
        handler(users, false)
    }



}//end func

1 个答案:

答案 0 :(得分:0)

对于您的地理查询范围内或位于范围内的每个键,都会触发.keyEntered事件。所以最初这意味着它会触发范围内的所有键,并且从那一刻起它将在新键进入范围时触发(即,如果您在范围内添加新用户,或者如果用户移动到范围内)。

听起来您想要检测何时为所有初始用户调用.keyEntered。为此,您可以观察就绪事件。

query.observeReadyWithBlock({
    //handler passing a shuffled version of the array
    handler(self.shuffleArray(array: filteredUsers) as! [User], true)
})

如果您对初始查询后获得新用户/移动用户的更新不感兴趣,这也是通过致电removeObserverWithFirebaseHandleremoveAllObservers来移除观察者的好时机。

另请参阅Geofire文档中的waiting for queries to be 'ready'