Firebase:何时在swift中调用removeObserverWithHandle

Use*_*156 15 observers uikit ios firebase swift

文档说observeEventType:withBlock如果您不再需要它,您需要调用以删除观察者.

我见过它被称为内容的样本ViewDidDisAppear.我也发现了一些称为此方法的Obj-C代码deinit,这在Swift中是不必要的.

但是,在我的简单应用程序中,只要我在应用程序中,就希望数据同步.如果是这种情况,我是否必须打电话observeEventType:withBlock

我检查了Firebase网站上的Chat-Swift示例代码,但没有找到observeEventType:withBlock.

这是否意味着不打电话observeEventType:withBlock:.如果我希望观察者在应用程序使用时打开?

谢谢.

UPDATE

感谢Jay和David.我认为在ViewWillAppear中观察并在ViewDidDisappear中删除它是有意义的.

但是,我使用observeEventType来监视节点的任何值更改,如果有任何更新UI.如果我把它放在ViewWillAppear中:

 override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    ref.observeEventType(.Value, withBlock: { snap in {
      // **update UI if there is any Value change** 
    })
  }
Run Code Online (Sandbox Code Playgroud)

将其放入的问题viewWillAppear是,无论值是否发生变化,每次出现视图时都会调用它.因此,每次返回视图时都会下载快照并刷新我的UI.这会适得其反.

我也试过ChildAdded/ ChildRemoved,但它只返回最后一个节点,而不是我的ref的路径:

例如,如果我添加到ref/child1/child2/child3/value,ChildAdded则只返回child3/value.

所以,如果我必须观察价值,似乎把它放进去ViewDidLoad更好?通过这种方式,它可以在加载视图时获取一次快照,并且只要有更改就会重复,但只是因为视图出现而无法获取快照.

Dav*_*ast 26

以@ Jay的优秀答案为基础:

在a中UIViewController,创建一个引用作为属性.初始化参考viewDidLoad.观察事件viewWillAppear.删除观察员viewDidDisappear.

class MyViewController: UIViewController {

  var ref: Firebase!

  // Called only on load, great place to initialize
  override func viewDidLoad() {
    super.viewDidLoad()
    ref = Firebase(url: "https://<YOUR-FIREBASE-APP>.firebaseio.com/updates")
  }

  // Can be called many times to go on screen
  // Syncing should only occur when on view to conserve memory
  override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    ref.observeEventType(.Value, withBlock: { snap in {
      // do something with the data 
    })
  }

  // Can be called many times to off screen
  // Remove observers on the ref to conserve memory
  override func viewDidDisappear(animated: Bool) {
    super.viewDidDisappear(animated)
    ref.removeAllObservers() 
  }

}
Run Code Online (Sandbox Code Playgroud)

根据你的编辑:

The problem with putting it in viewWillAppear is that, it gets called every time the view appears, regardless of Value change or not. Because of this, the snapshot is downloaded and my UI gets refreshed every time I return to the view. This becomes counterproductive.
Run Code Online (Sandbox Code Playgroud)

Firebase专为提高速度而设计.这些是你留给客户的东西,因为它有几个处理这些情况的功能.

Firebase客户端具有内置缓存.除非你在viewDidAppear更新中下载一兆字节的数据是名义上的.当观察者发射viewDidAppear它并不一定意味着它再次下载数据.该viewDidAppear功能是您的观察者所属的地方.

仅供参考,我是在iOS上工作的Firebase员工.


Jay*_*Jay 6

observeEventType:withBlock是用于观察节点的内容.

一旦应用程序观察节点,它将继续观察,除非您退出应用程序或告诉Firebase停止观察.

要停止观察,您可以使用开始观察时返回的句柄,如下所示:

    //start observing and get a handle
FirebaseHandle handle = [ref observeEventType:FEventTypeValue withBlock:^(FDatasnapshot* snapshot) {
        // do some stuff with the snapshot data
    }];

    [ref removeObserverWithHandle:handle]; //stop observing using the handle
Run Code Online (Sandbox Code Playgroud)

或者像这样

[ref removeAllObservers];
Run Code Online (Sandbox Code Playgroud)