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 ?
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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With