如何删除 Swift 中以下观察的 GeoFire 句柄?

How to remove a GeoFire handle for the following observe in Swift?

这是 Swift、Firebase 和 Geofire 的问题。

我想知道如何为 Swift 中的以下观察者删除 GeoFire 句柄。

locationsEnd!.query(at: location, withRadius: 16.0).observe(GFEventType.init(rawValue: 0)!, with: {(key, location) in

以下工作正常(在 viewDidDisappear 中):

locationsEnd?.firebaseRef.removeAllObservers()

但是它没有句柄:

var locationHandle: UInt = 0

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)

    //following does not work:
    locationsEnd?.firebaseRef.removeObserver(withHandle: locationHandle)
}

func aroundMe(_ location: CLLocation){

        locationHandle = locationsEnd!.query(at: location, withRadius: 16.0).observe(GFEventType.init(rawValue: 0)!, with: {(key, location) in

            //etc
        })
}

我尝试了如下的 locationHandle,但没有成功:

var locationHandle = FirebaseHandle()
var locationHandle: FirebaseHandle = 0
var locationHandle: UInt!
var locationHandle: UInt = 0
var locationHandle = FIRDatabaseHandle()
var locationHandle: FirebaseHandle = 0

任何建议都很好,如前所述,我可以删除所有观察者,但在其他地方我只需要删除一个句柄。

locationHandle 在您的代码中被定义为一个 UInt,它需要是一个 FIRDatabaseHandle,所以

下面是删除 Firebase 句柄的示例

var myPostHandle : FIRDatabaseHandle?

func someFunc()
{
    myPostHandle = ref.child("posts").observeEventType(.childAdded,
      withBlock: { (snapshot) -> Void in

            let postBody = snapshot.value!["body"] as! String

    })
}

func stopObserving()
{
    if myPostHandle != nil {
        ref.child("posts").removeObserverWithHandle(myPostHandle)
    }
}

GeoFire 更像这样

let geofireRef = FIRDatabase.database().reference()
let geoFire = GeoFire(firebaseRef: geofireRef)
let center = CLLocation(latitude: 37.7832889, longitude: -122.4056973)
var circleQuery = geoFire.queryAtLocation(center, withRadius: 0.6)

var queryHandle = circleQuery.observeEventType(.KeyEntered,
         withBlock: { (key: String!, location: CLLocation!) in
             //do something
})

然后删除,使用

circleQuery.removeObserverWithFirebaseHandle(queryHandle)