Should FirebaseDatabase.getInstance() be used sparingly?

浪子不回头ぞ 提交于 2019-12-31 04:05:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!