Firebase:何时在swift中调用removeObserverWithHandle [英] Firebase: when to call removeObserverWithHandle in swift

查看:320
本文介绍了Firebase:何时在swift中调用removeObserverWithHandle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档说,如果你不再需要观察者,你需要调用 observeEventType:withBlock 来移除观察者。

我已经在 ViewDidDisAppear 中看到过样本。我还在 deinit 中找到了一些Obj-C代码,这在Swift中是不需要的。



我的简单的应用程序,但是,我希望数据被同步,只要我在应用程序。如果是这种情况,是否必须调用 observeEventType:withBlock

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

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

谢谢。

更新

感谢Jay和David。我发现在ViewWillAppear中观察并在ViewDidDisappear中删除它是有意义的。然而,我正在使用observeEventType来监视节点的任何值更改,如果有任何更改UI将更新。如果我把它放在ViewWillAppear中:
$ b $ pre $ override func viewWillAppear(animated:Bool){
super.viewWillAppear(animated)

ref.observeEventType(.Value,withBlock:{snap in {
// **更新界面,如果有任何值更改**
})
}

把它放在 viewWillAppear 中的问题是那么,每次出现视图时都会调用它,而不管值是否更改。因此,下载快照,每当我返回到视图时,我的用户界面都会刷新。这变得适得其反。



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

例如,如果我添加到ref / child1 / child2 / child3 /值, ChildAdded 只会返回child3 /值。所以如果我必须遵守价值观,好像把它放在 ViewDidLoad 更好?通过这种方式,当视图加载时,它会获得一次快照,并且只要有变化就会重复,但不会因为视图出现而获得快照。

解决方案

在@ Jay的优秀回答中加入:

UIViewController ,创建一个引用作为一个属性。初始化 viewDidLoad 中的引用。观察 viewWillAppear 中的事件。删除 viewDidDisappear 中的观察者。
$ b

  class MyViewController:UIViewController {

var ref:Firebase!

//只调用负载,初始化
的好地方覆盖func viewDidLoad(){
super.viewDidLoad()
ref = Firebase(url:https ://< YOUR-FIREBASE-APP> .firebaseio.com / updates)
}

//可以多次调用屏幕
//同步应该只发生在保存内存时
重写func viewWillAppear(animated:Bool){
super.viewWillAppear(animated)

ref.observeEventType(.Value,withBlock:{ snap in {
//用数据
做些事情})
}

//可以多次调用off-screen
// Remove观察员在节省内存
重写func viewDidDisappear(animated:Bool){
super.viewDidDisappear(动画)
ref.removeAllObservers()
}


$ / code>

您的编辑:

 把它放在viewWillAppear中的问题是,它每次都被调用视图出现,不管值是否改变。因此,下载快照,每当我返回到视图时,我的用户界面都会刷新。这变得适得其反。 

Firebase是为速度而构建的。这些是您留给客户端的东西,因为它有几个功能来处理这些情况。



Firebase客户端具有内置缓存。除非你在 viewDidAppear 中下载了兆字节的数据,否则更新是名义上的。当观察者在 viewDidAppear 上触发时,并不一定意味着它再次下载数据。 viewDidAppear 函数是您观察者所属的地方。

仅供参考,我是一名在iOS上工作的Firebase员工。

Documentation says you need to call observeEventType:withBlock to remove an observer if you no longer need it.

I've seen samples where it is called within ViewDidDisAppear. I also find some Obj-C code called this method within deinit, which is not ncessary in Swift.

In my simple app, however, I want data to be synced as long as I am in the app. If this is the case, do I have to call observeEventType:withBlock ever?

I checked the Chat-Swift sample code on Firebase website, and did not find observeEventType:withBlock.

Does it mean it's ok not to call observeEventType:withBlock:. if I want the observer to be on when the app is in use?

Thank you.

UPDATE

Thanks to Jay and David. I see it makes sense to observe in ViewWillAppear and remove it in ViewDidDisappear.

However, I am using observeEventType to monitor any value change to the node and would update UI if there is any. If i put it in ViewWillAppear:

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

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

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.

I have also tried ChildAdded/ChildRemoved, however, it only returns the last node, not the path from my ref:

For instance, if I add to ref/child1/child2/child3/value, ChildAdded would only return child3/value.

So if I have to observe Value, it seems like putting it in ViewDidLoad is better? In this way, it gets the snapshot one time when the view loaded, and would repeat whenever there is a change, but would not obtain the snapshot just because the view appears.

解决方案

To build upon @Jay's excellent answer:

In a UIViewController, create a reference as a property. Initialize a reference in viewDidLoad. Observe events in viewWillAppear. Remove observers in 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() 
  }

}

Per your edit:

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.

Firebase is built for speed. These are the kind of things that you leave up to the client because it has several features that handle these situations.

The Firebase client has built-in caching. Unless you're downloading a megabyte of data in viewDidAppear the update is nominal. When the observer fires on viewDidAppear it doesn't necessarily mean it's downloading the data again. The viewDidAppear function is where your observers belong.

FYI, I am a Firebase employee who works on iOS.

这篇关于Firebase:何时在swift中调用removeObserverWithHandle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆