junGyeom Kim
junGyeom Kim

Reputation: 29

Android Firebase realtime database get data is so slow

When I was using firebase for initialization of data and setText on UI, It was delayed for 10~15 seconds. On the other hand, When I use Volley, It was delayed for only 1 seconds.

why is it so slow when i'm using firebase and How can I modifying this code?

here is my code (using ValueEventListener) and JSON tree

"couple_profile" : {
"TEST999AhCBcHK32AQzU3JglVbAxhTD6Mn1" : {
  "birthday_1" : "1994-06-29",
  "birthday_2" : "19940629",
  "couple_nick" : "TEST999",
  "exp" : 0,
  "gold" : 0,
  "level" : 1,
  "member" : {
    "AhCBcHK32AQzU3JglVbAxhTD6Mn1" : {
      "FCM" : "d5Y-fl_0VCs:APA91bHX7HA531PXc1e4clUg61uB3XsKlwmQ_2U9OaESUTP0r-dnwbitSvxQ4EDMqPzK-t_5b9qPOhLm01FRffgB9-Ot6bZmx1JzwZvc07yoyhashGUS79E7Dztr2J7NfR1NGXw1LT2V",
      "birthday" : "1994-06-29",
      "name" : "김준겸",
      "phone_number" : "01050639201"
    },
    "FMsH7leNTOXBgbOFoWdaTMjt05T2" : {
      "birthday" : "19940629",
      "name" : "kimGod",
      "phone_number" : "1541"
    }
  },
  "mileage" : 0,
  "name_1" : "김준겸",
  "name_2" : "kimGod",
  "uid_1" : "AhCBcHK32AQzU3JglVbAxhTD6Mn1",
  "uid_2" : "FMsH7leNTOXBgbOFoWdaTMjt05T2"
}


private void get_home_info() {
    valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(final DataSnapshot dataSnapshot) {
            coupleProfile = dataSnapshot.getValue(CoupleProfile.class);
            if (coupleProfile == null) {
                Log.e(TAG, "User " + FirebaseAuth.getInstance().getCurrentUser().getEmail() + " is unexpectedly null_1");
                Toast.makeText(Home_Fragment.this.getContext(), "Error_1", Toast.LENGTH_SHORT).show();
            } else {

                StorageReference storageRef2 = storage.getReferenceFromUrl("FirebaseURL").child("Profile_image/" + FirebaseAuth.getInstance().getCurrentUser().getUid());

                Glide.with(getContext()).using(new FirebaseImageLoader()).load(storageRef2).centerCrop()
                        .bitmapTransform(new CropCircleTransformation(new CustomBitmapPool()))
                        .into(me_view);
                SharedPreferences.Editor sh = home_info.edit();

                if (My_UID.equals(coupleProfile.uid_1)) {
                    my_number = 1;
                    sh.putString("my_name",coupleProfile.name_1);
                    sh.putString("op_name",coupleProfile.name_2);
                    sh.apply();
                    my_name_text.setText(coupleProfile.name_1);
                    op_name_text.setText(coupleProfile.name_2);
                    state_text.setText(coupleProfile.state_1);
                    op_state_text.setText(coupleProfile.state_2);
                    StorageReference storageRef = storage.getReferenceFromUrl("FirebaseURL").child("Profile_image/" + coupleProfile.uid_2);
                    Glide.with(getContext()).using(new FirebaseImageLoader()).load(storageRef).centerCrop()
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .bitmapTransform(new CropCircleTransformation(new CustomBitmapPool()))
                            .into(friend_view);

                } else if (My_UID.equals(coupleProfile.uid_2)) {
                    my_number = 2;
                    sh.putString("my_name",coupleProfile.name_2);
                    sh.putString("op_name",coupleProfile.name_1);
                    sh.apply();
                    my_name_text.setText(coupleProfile.name_2);
                    op_name_text.setText(coupleProfile.name_1);
                    state_text.setText(coupleProfile.state_2);
                    op_state_text.setText(coupleProfile.state_1);

                    StorageReference storageRef = storage.getReferenceFromUrl("FirebaseURL").child("Profile_image/" + coupleProfile.uid_1);

                    Glide.with(getContext()).using(new FirebaseImageLoader()).load(storageRef).centerCrop().diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .bitmapTransform(new CropCircleTransformation(new CustomBitmapPool()))
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .into(friend_view);


                } else {
                    Log.e(TAG, "User " + FirebaseAuth.getInstance().getCurrentUser().getEmail() + " is unexpectedly null_2");
                    Toast.makeText(Home_Fragment.this.getContext(), "Error", Toast.LENGTH_SHORT).show();
                }

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, "onCancelled: ", databaseError.toException());

        }
    };
    ref = databaseReference.child("couple_profile").child(room_token);
    ref.addValueEventListener(valueEventListener);
}

Upvotes: 1

Views: 5030

Answers (2)

Chris Brandow
Chris Brandow

Reputation: 73

Also, look for uses of .keepSynced(true). This likewise causes the entire node for which it is called to be fetched before it makes any updates. This can be very slow, depending on what node it is called for.

Upvotes: 0

Peter Haddad
Peter Haddad

Reputation: 80914

I think if you use this method from firebase you will have better loading of data:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

Use the above code, explanation about it:

Firebase provides great support when comes to offline data. It automatically stores the data offline when there is no internet connection. When the device connects to internet, all the data will be pushed to realtime database. However enabling disk persistence stores the data offline even though app restarts.

Also try and retrieve the data in onStart()

Upvotes: 4

Related Questions