这是一个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
任何建议都会很好,正如前面提到的,我可以移除所有的观察者,但是在其他地方,我需要移除一个句柄。
发布于 2017-01-03 10:51:59
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)
https://stackoverflow.com/questions/41449938
复制相似问题