使用removeObserver(在Swift 3中使用withHandle,在viewDidDisappear上不删除Observer
var query = FIRDatabaseQuery()
var postRef: FIRDatabaseReference!
var postRefHandle: FIRDatabaseHandle?
override func viewDidLoad() {
super.viewDidLoad()
postRef = baseRef.child("Posts")
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
if postRefHandle != nil {
//try 1:
//postRef.removeObserver(withHandle: postRefHandle!)
//try 2:
//postRef.queryOrdered(byChild: "sortTimestamp").removeObserver(withHandle: postRefHandle!)
//try 3:
//query.removeObserver(withHandle: postRefHandle!)
}
//try 4:
//postRef.removeAllObservers() //works
}
func getPosts()
{
var count = 20
query = postRef.queryOrdered(byChild: "sortTimestamp")
postRefHandle = query.queryLimited(toFirst: UInt(count)).observe(.childAdded //etc.
}
Run Code Online (Sandbox Code Playgroud)
所以我尝试了viewDidDisappear中的三个方法,但是没有删除观察者.尝试3来自Firebase的 query.removeObserver(withHandle:postRefHandle!),如何返回句柄以便我可以调用removeObserver?作者:frank-van-puffelen
唯一有效的是try 4中概述的那个.我无法用removeObserver删除Observer的任何原因(withHandle?(try 1 - 3)
另外"query.queryLimited(toFirst:UInt(count)).观察(.childAdded"没有从Firebase获取最新数据.我认为观察总是得到更新的数据,而不是observeSingleEvent.为什么不是去做? …
这是 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 …Run Code Online (Sandbox Code Playgroud)