关于 Swift 观察者的 Firebase 文档要么有点过时,要么对我来说不太清楚。我仍然不太明白到底什么时候删除观察者。那么observeSingleEvent 的情况又如何呢?另外,删除这些观察者的正确方法在哪里?
最佳答案
Firebase's documentation没有指定这一点,但它似乎是隐含的,所以我不会删除它:
Read data once
In some cases you may want a callback to be called once and then immediately removed, such as when initializing a UI element that you don't expect to change. You can use the observeSingleEventOfType method to simplify this scenario: the event callback added triggers once and then does not trigger again.
要删除观察者,请先将其附加到句柄,然后使用该句柄作为引用:
// Add observer
let handle = ref.observeSingleEvent(of: .value, with: { value in
// ...
})
// Then remove it
ref.removeObserver(withHandle: handle);
// Or just remove all of them
ref.removeAllObservers()
关于ios - 即使它是单个事件,我是否应该删除 Firebase 观察者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41077497/