Skip to content Skip to sidebar Skip to footer

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 t

Solution 1:

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.

classMainActivityextendsAppCompatActivity {
    Firebase mRef;

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

classMainActivityextendsAppCompatActivity {
    Firebase mRef;
    Firebase mUsersRef;
    Firebase mPostsRef;

    ...
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(...);
        ...
        mRef = newFirebase("https://yours.firebaseio.com");
        mUsersRef = mRef.child("users");
        mPostsRef = mRef.child("posts");
        ...
    }

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

Solution 2:

1)Personally i am not using single ton for firebase 2).Don't use the second one.may be sometimes you want a dynamic path variable.. 3) I think 3 n 4 has the same effect..

Solution 3:

I'm doing it this way.! this method also helps me to get unknown child key adn its value access.!

  mdatabaseRef.child("users").orderByKey().equalTo(key).addValueEventListener(newValueEventListener() {
                        @OverridepublicvoidonDataChange(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(newValueEventListener() {
      @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {

      Log.e(TAG, "::Post::Child:2"+dataSnapshot.getKey());

      for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {


        final Post post= postSnapshot.getValue(Post.class);

Post a Comment for "Best Way To Handle Firebase References With Android"