firebase对象应该是一个快速的单身人士吗? [英] should the firebase object be a singleton in swift?

查看:145
本文介绍了firebase对象应该是一个快速的单身人士吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ios的Swift中实现firebase sdk,firebase对象是否应该存储在一个singleton中?还是它自动管理和关闭视图之间的连接?



例如,在每个视图中创建一个firebase对象创建多个套接字连接?只保留一个firebase对象会更高效吗?

  let ref = Firebase(url:https://+ kFireBaseHost +.firebaseio.com /)


解决方案

不需要做一个单身人士。在幕后,Firebase管理一个连接,如果您有多个Firebase对象,或者即使您在一个位置有多个观察者,也会进行相应的重复数据删除。如果您为每个视图创建一个新的Firebase对象,只要它使用相同的基本URL,它仍将使用与服务器相同的连接。



您需要管理您有多少个Firebase对象,您应该管理您的观察者。这些不会被删除之间的意见。要移除观察者,可以使用 observeEventType 方法返回的 FirebaseHandle ,使用 removeObserverWithHandle: 方法或调用 removeAllObservers 。请注意,这两种方法都要求您在与观察者相连的位置相同的URL位置调用它们(尽管它不必是相同的对象,只是相同的URL)。如果您不移除观察者,您可能会看到他们从您离开的视图中触发,因为其他人正在更改数据。您可以在拆离块下的文档中阅读更多



这对于Swift或Objective-C都是如此。

in the ios Swift implementation of the firebase sdk, should the firebase object be stored in a singleton? Or does it manage and close connections automatically between views?

For instance, would creating a firebase object in each view create multiple socket connections? Would it be more efficient to just keep one firebase object?

let ref = Firebase(url: "https://" + kFireBaseHost + ".firebaseio.com/")

解决方案

You don't need to make a singleton. Behind the scenes, Firebase manages a single connection and will dedupe appropriately if you have multiple Firebase objects or even if you have multiple observers at a single location. If you create a new Firebase object per view, so long as it's using the same base url, it will still use the same connection to the server.

While you don't need to manage how many Firebase objects you have, you should manage your observers. These do not get removed between views. To remove observers, you can use the FirebaseHandle returned by the observeEventType methods with the removeObserverWithHandle: method or call removeAllObservers. Note that both these methods require you call them at the same url location as the place you attached the observer (although it need not be the same object, just the same url). If you don't remove the observers, you might see them triggering from a view you left because someone else is changing the data. You can read more in the docs under Detaching Blocks.

This is all true for Swift or Objective-C.

这篇关于firebase对象应该是一个快速的单身人士吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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