如何不断查询最近的用户?

时间:2019-07-01 02:10:41

标签: swift firebase firebase-realtime-database location geofire

这些功能来自两个不同的服务文件。在我的updateUserLocation中,我不断更新当前用户的位置,并查看位置服务中的showClosestUser函数。我现在正尝试使用100的半径进行查询(以进行测试,最好是半径为5或10的较小半径)。添加断点时,我看到不断调用updateUserLocation,但是对于showClosestUser函数,代码将运行到“ myQuery?.observe(.keyEntered,带有:{(键,位置)在”中,并且不执行“ UserService .show(forUID:键,完成:{(用户)在“代码中?我试图查看是否查询了该用户?(是正确的)不是当前用户,所以我可以进行createNotification。为什么不该代码正在执行?

static func updateUserLocation(location: CLLocation){
    var geoFireRef: DatabaseReference?
    var geoFire: GeoFire?

    geoFireRef = Database.database().reference().child("Geolocs")
    geoFire = GeoFire(firebaseRef: geoFireRef!)
    geoFire?.setLocation(location, forKey:(Auth.auth().currentUser?.uid)!)

    LocationService.showClosestUser { (user) in
        if user != nil {
            UserService.createNotification()
        }
   }
}

static func showClosestUser( completion: @escaping (User) -> ()){
    var geoFireRef: DatabaseReference?
    var geoFire: GeoFire?
    var myQuery: GFQuery?

    geoFireRef = Database.database().reference().child("Geolocs")

    geoFire = GeoFire(firebaseRef: geoFireRef!)

    let userLat = UserDefaults.standard.value(forKey: "current_latitude") as! String
    let userLong = UserDefaults.standard.value(forKey: "current_longitude") as! String

    let location:CLLocation = CLLocation(latitude: CLLocationDegrees(Double(userLat)!), longitude: CLLocationDegrees(Double(userLong)!))

    myQuery = geoFire?.query(at: location, withRadius: 100)

    myQuery?.observe(.keyEntered, with : { (key, location) in

        UserService.show(forUID: key, completion: { (user) in
            if let user = user {
                if (user.uid != User.current.uid){
                    completion(user)
                }
            }
            else {return}
        })
    })
}

0 个答案:

没有答案