Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to handle Firebase references with Android

When you deal with Firebase data (read, write...) in an Android app, you need to get the firebase reference to then be able dealing with data.

As the Firebase reference is a JSON tree, if you point the tree root, you can then always access a child, doesn't matter the deepness.

Question : what is the best way from memory, and latency point of view to handle this reference in the code ?

  • Root
    • C1
      • C10
      • C11
    • C2
      • C21

1/ Create a static Firebase ref to the root in the Application.

MyApplication.getFirebaseRootRef().chid(C1).chid(C11).setValue(...); 

2/ Create a new firebase ref for the child C11

Firebase ref = new Firebase("https://your.firebaseio.com/C1/C11");
ref..setValue(...);

3/ Hybrid

Firebase ref = new Firebase("https://your.firebaseio.com");
ref.child(C1).child(C11).setValue(...);

4/ Hybrid 2

Firebase ref = new Firebase("https://your.firebaseio.com").child(C1).child(C11);
ref.setValue(...);

Is there any difference in performance ?

Maybe you may have some advises for readability and maintenance ?

like image 288
Anthony Avatar asked Oct 16 '15 09:10

Anthony


1 Answers

Firebase queries and references are lightweight objects. The heavy lifting is done behind the scenes by classes that are internal to (and managed by) the Firebase SDK itself.

Because of this, there will be no significant difference in performance between any of the approaches you proposed.

Personal preference below

I usually keep a reference as a member in each activity.

class MainActivity extends AppCompatActivity {
    Firebase mRef;

If I have more primary list types, I'll add members for those:

class MainActivity extends AppCompatActivity {
    Firebase mRef;
    Firebase mUsersRef;
    Firebase mPostsRef;

    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(...);
        ...
        mRef = new Firebase("https://yours.firebaseio.com");
        mUsersRef = mRef.child("users");
        mPostsRef = mRef.child("posts");
        ...
    }

By putting everything in each activity, those are nicely self-contained.

like image 110
Frank van Puffelen Avatar answered Sep 25 '22 15:09

Frank van Puffelen