简体   繁体   English

检查我是否有观察者在Firebase Swift中监听

[英]Check if I have an observer listening in firebase swift

Before detaching a firebase handle or removing all observers, I want to check if there are observers listening in my app or I want to check if specific handle/observer is listening. 在分离Firebase句柄或删除所有观察者之前,我想检查我的应用程序中是否有观察者在听,或者我想检查特定的句柄/观察者是否在听。 How do I achieve this? 我该如何实现?

I could not find a firebase api to achieve this. 我找不到Firebase API来实现这一目标。 I solved it using this workaround 我使用此解决方法解决了它

var handle = ref.child("child").observe()

// check if nil before detaching listener
guard self.handle != nil else { return }
self.ref.removeObserver(withHandle: self.handle)

Alternatively, 或者,
If you are not setting multiple observers on the same reference, you can simply keep track of all your references with observers and just call removeAllObservers() on each of them. 如果您没有在同一个引用上设置多个观察者,则只需使用观察者跟踪所有引用,然后对每个观察者调用removeAllObservers() That way you don't have to keep track of handles and worry about checking for nil values. 这样,您就不必跟踪句柄并担心检查nil值。
For example, 例如,

var handle = ref.child("child").observe()
handle.removeAllObservers()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM