问题
For example when I use a SQLiteDatabase in android, it is generally not a good idea to open/close lots of SQLiteDatabase helpers. Instead it is better to create a sort of singleton which makes sure you only have 1 database open.
Say I have a class with static methods doing lots of Firebase operations which require a DatabaseReference. example:
static void checkIfUserIsMatched(...)
static void notifyUser(...)
static void modifyUser(...)
Is it OK to call FirebaseDatabase.getInstance() inside every method? Create a new DatabaseReference inside each method?
or would it be better to pass the DatabaseReference from the calling Activity?
static void notifyUser(mUserReference, hisUserId)
vs
static void notifyUser(myUserId, hisUserId) --> Create the reference inside method
What im looking to optimize is network usage and performance. Or does this all not matter and does firebase handle everything for me?
回答1:
FirebaseDatabase and DatabaseReference objects can be considered relatively cheap references to the underlying resources.
The Firebase SDK manages such things behind the scenes. Only the first call to FirebaseData.getInstance() will do the set up work, subsequent calls (within the same process) will simply re-use what was done already.
来源:https://stackoverflow.com/questions/39109616/should-firebasedatabase-getinstance-be-used-sparingly