Managing instantiating firebase products in an iOS app. Is there a practical limit / best practices to know about?

Viewed 15

I'm curious what are the side effects of having a single instance of a Firebase product as opposed to reinitializing them as needed? What is the general rule of thumb? Is it generally better to have several or fewer instances? Why or why not?

In my latest round of refactoring my app, it occured to me I could create a singleton class containing a single instance of every firebase product. But I have a vague memory of reading somewhere that you should try to minimize the amount of instances as possible, particularly with the Auth instance. Is this true? This makes sense for Auth but I could see issues with the others, at least in my app's case (relatively high quantity of content fetched and displayed at a time including firestore docs and images).

import Firebase

class FBServiceProvider {
    static let shared = FBServiceProvider()
    private init() { }
    
    let auth = Auth.auth()
    let stg = Storage.storage()
    let db = Firestore.firestore()
    let fn = Functions.functions()
    let rc = RemoteConfig.remoteConfig()
    let msg = Messaging.messaging()
}
1 Answers

The service objects are fairly lightweight, so there is no need to go out of your way to limit yourself to just having one instance.

I tend to have an instance of each service in each major class that needs it, and have never seen performance problems because of that.

The important thing to realize may be that creating these service objects doesn't cause any network traffic, so any performance gains you get from reusing them is gonna be dwarfed by the first call you make against the service that does need to make a network call.

Related