隨着removeObserver(withHandle斯威夫特卡倫特3,觀察者不viewDidDisappear刪除隨着火力地堡,斯威夫特removeObserver(withHandle不刪除觀察者
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.
}
於是,我就在viewDidDisappear的三種方法,但觀察者。不會刪除 嘗試3 query.removeObserver(withHandle:postRefHandle)通過回答Firebase, how do I return a handle so that I can call removeObserver?由Frank-VAN-puffelen
,做的工作是一個在嘗試4 任何理由列舉了爲什麼我不能只有一個用removeObserver刪除Observer(withHandle? (嘗試1 - 3)
此外,「query.queryLimited(toFirst:UInt(count))。observe(.childAdded」沒有從Firebase獲取最新的數據,我留下的印象是觀察總是得到更新的數據,而不是observeSingleEvent爲何不這樣做
任何建議都非常讚賞
也許'postRefHandle'有問題。 –
@ArchieGertsman你是什麼意思?手柄會出現什麼問題? –
也許它是'nil',整個'if'塊沒有被執行。嘗試在塊中打印一些東西。 –