ios - 即使它是单个事件,我是否应该删除 Firebase 观察者?

标签 ios swift firebase firebase-realtime-database

关于 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/

相关文章:

javascript - 刷新页面图像后,AngularJS 的 View 中没有显示?

ios - 两个应用共享相同的通用链接

ios - 隐藏图像并稍后绘制 objective-c

swift - 如何获取指向 Swift 结构的 C 指针的值?

ios - 将核心数据与服务器响应同步

ios - RxSwift 访问 indexPath.section

IOS - 如何在 Core Data Swift 中将数据插入具有关系的不同表中

firebase - 使用 Mockito 为 Firebase 用户身份验证设置单元测试

objective-c - Objective-C : How to use addTarget:action:forControlEvents: method?

iphone - UILabel 超出应用程序框架