Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Firebase Analytic's getInstance() every time vs. storing instance as a static variable in Application class

I'm trying decide which of the following is the proper way to do this:

  1. Calling FirebaseAnalytics.getInstance(Context) from every activity, fragment, and service that I'm logging an event from.

or

  1. Calling FirebaseAnalytics.getInstance(Context) once from Application class and keeping it around as a public static variable. Then, from everywhere I need this I can call `MyAppClass.mFirebaseAnalytics.logEvent()'.

Will any of the above methods have a undesired impact on the events that are automatically collected and/or do either of those have an efficiency gain over the other?

Many thanks!

like image 897
fahmy Avatar asked Oct 02 '16 03:10

fahmy


1 Answers

The documentation states:

public static FirebaseAnalytics getInstance (Context context)

Returns the singleton FirebaseAnalytics interface.

So I don't see any particular reason why you can't just have a singleton instance in your code. There won't be any noticeable efficiency gains with either approach. If you're looking into the second option, it might be worth considering doing this with dependency injection and a simple wrapper around the analytics instance to increase the testability of your code.

like image 101
bcpettifer Avatar answered Oct 24 '22 12:10

bcpettifer