The best way to handle Firebase links with Android

When you work with Firebase data (read, write ...) in an Android application, you need to get a link to firebase in order to be able to process the data.

Since the Firebase link is a JSON tree, if you specify the root of the tree, you can always refer to the child, no matter how deep.

Question: what is the best for memory and latency for processing this link in code?

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

1 / Create a static Firebase for the root in the application.

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

2 / Create a new firebase ref for baby 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 a difference in performance?

Maybe you can have some tips for easy reading and maintenance?

+5
source share
3 answers

Firebase queries and links are lightweight objects. Heavy lifting is done behind the scenes for classes that are internal (and managed) by the Firebase SDK itself.

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

Personal preferences below

I usually save the link as a member in every action.

 class MainActivity extends AppCompatActivity { Firebase mRef; 

If I have more primary list types, I will add the following elements for them:

 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"); ... } 

Having placed everything in each type of activity, they are perfectly self-sufficient.

+5
source

1) Personally, I do not use one ton for firebase 2). Do not use the second. Sometimes you need a dynamic path variable. 3) I think 3 n 4 has the same effect.

0
source

I do so. this method also helps me get the unknown child key and access its value.!

  mdatabaseRef.child("users").orderByKey().equalTo(key).addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if(lList.size()>0) lList.clear(); for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) { if(postSnapshot.getKey()!=null) { Log.e(TAG, "::User:Child:1"+postSnapshot.getKey()); mdatabaseRef.child("Users") .child(postSnapshot.getKey()) .addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { Log.e(TAG, "::Post::Child:2"+dataSnapshot.getKey()); for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) { final Post post= postSnapshot.getValue(Post.class); 
0
source

Source: https://habr.com/ru/post/1233850/


All Articles