Ios 如何在Swift中移除GeoFire手柄以进行以下操作?
这是Swift、Firebase和Geofire的问题 我想知道如何在Swift中为以下观察者移除GeoFire手柄Ios 如何在Swift中移除GeoFire手柄以进行以下操作?,ios,swift,firebase,geofire,Ios,Swift,Firebase,Geofire,这是Swift、Firebase和Geofire的问题 我想知道如何在Swift中为以下观察者移除GeoFire手柄 locationsEnd!.query(at: location, withRadius: 16.0).observe(GFEventType.init(rawValue: 0)!, with: {(key, location) in 以下工作正常(在视图中): 但是,使用handle时,它不会: var locationHandle: UInt = 0 override f
locationsEnd!.query(at: location, withRadius: 16.0).observe(GFEventType.init(rawValue: 0)!, with: {(key, location) in
以下工作正常(在视图中):
但是,使用handle时,它不会:
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)
非常感谢你的回复。刚刚试过,似乎没什么区别。我试过:var locationHandle:FIRDatabaseHandle?变量locationHandle:FIRDatabaseHandle!var locationHandle=FIRDatabaseHandle()var locationHandle:FIRDatabaseHandle=0@PeterdeVries在我的答案中添加了一些快速示例代码,已经过测试。非常感谢,事实上,我在其他地方也有相同的结构,效果非常好。问题在于卸下GeoFire手柄。removeAllObservers()可以工作,removeObserver(带句柄:locationHandle)不能工作。@PeterdeVries哦,我键入了fast。请使用removeObserverWithFirebaseHandle而不是removeObserverWithHandle。我得到:类型为“FIRDatabaseReference”的值没有成员“removeObserverWithFirebaseHandle”。尝试将其更改为removeObserver(使用FirebaseHandle:locationHandle),但swift要求我像以前一样放置(removeObserver(使用Handle)
circleQuery.removeObserverWithFirebaseHandle(queryHandle)