Pet*_*ies 1 ios firebase swift geofire
这是 Swift、Firebase 和 Geofire 的问题。
我想知道如何在 Swift 中删除以下观察者的 GeoFire 句柄。
locationsEnd!.query(at: location, withRadius: 16.0).observe(GFEventType.init(rawValue: 0)!, with: {(key, location) in
Run Code Online (Sandbox Code Playgroud)
以下工作正常(在 viewDidDisappear 中):
locationsEnd?.firebaseRef.removeAllObservers()
Run Code Online (Sandbox Code Playgroud)
然而,对于句柄来说,它不会:
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
})
}
Run Code Online (Sandbox Code Playgroud)
我尝试了如下locationHandle,但没有成功:
var locationHandle = FirebaseHandle()
var locationHandle: FirebaseHandle = 0
var locationHandle: UInt!
var locationHandle: UInt = 0
var locationHandle = FIRDatabaseHandle()
var locationHandle: FirebaseHandle = 0
Run Code Online (Sandbox Code Playgroud)
任何建议都会很好,正如前面提到的,我可以删除所有观察者,但在其他地方我需要删除一个句柄。
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)
}
}
Run Code Online (Sandbox Code Playgroud)
对于 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
})
Run Code Online (Sandbox Code Playgroud)
然后要删除,请使用
circleQuery.removeObserverWithFirebaseHandle(queryHandle)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
916 次 |
| 最近记录: |