Reputation: 47901
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/")
Upvotes: 3
Views: 2701
Reputation: 2030
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.
Upvotes: 9