我有这样的Firebase处理程序:
private var typeIndicatorHandle: DatabaseHandle?
self.typeIndicatorHandle = self.dbRef.child("chats").child(chatId).child("typingIndicator").queryOrderedByValue().queryEqual(toValue: true).observe(.value, with: { (snapshot) in
print("new value")
})
在其他地方我这样做:
if let typeIndicatorHandle = self.typeIndicatorHandle {
self.dbRef.removeObserver(withHandle: typeIndicatorHandle)
}
现在问题是观察者仍然获得新值。怎么可能?
答案 0 :(得分:1)
您需要删除附加原始子项的观察者。
例如:
private var typeIndicatorHandle: DatabaseHandle?
private var dbRef:DatabaseReference?
self.childRef= self.dbRef.child("chats").child(chatId).child("typingIndicator").queryOrderedByValue().queryEqual(toValue: true)
self.typeIndicatorHandle = childRef.observe(.value, with: { (snapshot) in
print("new value")
})
删除侦听器:
if let typeIndicatorHandle = self.typeIndicatorHandle {
self.childRef.removeObserver(withHandle: typeIndicatorHandle)
}
抱歉我的语法错误。如果有任何正确的话,我不知道那么快。
但您需要删除已添加DatabaseReference
的{{1}}上的侦听器。