0

I am currently in a bit of a struggle. I have been coding a basic social media app for quite a while now, and the images are loading slowly. I am using picasso to load images from my firebase storage and it is quite slow.

When the app is started the posts that I have loaded into a recyclerview only load when I scroll over them, but they load relatively fast. As I do more stuff, switching activities etc my images will load slower and slower until they won't load and I am stuck with my placeholder image.

It is not only the recyclerviews because I have a settings activity to display a user's credentials, and I get the same slow loading image. Here is some code.

  UserRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.hasChild("username")){
                String Username =  dataSnapshot.child("username").getValue().toString();
                username.setText(Username);
            }
            if (dataSnapshot.hasChild("profileimage")){
                String image = dataSnapshot.child("profileimage").getValue().toString();
                Picasso.get().load(image).placeholder(R.drawable.user_circle2).into(profileImage);
            }
        }

The text loads instantly, no matter how much activity the user has on the app, but the images continually load slower.

Please can someone help, or suggest an alternate method for quickly displaying images, Thanks.

EDIT: Thanks from Doug Stevenson for telling me to remove listeners, would I do it like this?

  eventListener = UserRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.hasChild("username")){
                String Username =  dataSnapshot.child("username").getValue().toString();
                username.setText(Username);
            }
            if (dataSnapshot.hasChild("profileimage")){
                String image = dataSnapshot.child("profileimage").getValue().toString();
                Picasso.get().load(image).placeholder(R.drawable.user_circle2).into(profileImage);
                endListener();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    }

private void endListener() {
    UserRef.removeEventListener(eventListener);
}

UPDATE: I have added the remove listeners to the app. I was debugging this on my phone, and of course I got the slow images as mentioned before, I tried this on an emulator though and the speed of the image loading stayed the same, and I could switch activities and even after I did quite a few things, the images were all loading ok when I switched back to my posts. Any ideas why???

I am also now getting the following message in the logcat:

Starting a blocking GC Alloc 05-07 19:28:20.493 15034-16164/com.sender.hp.sender I/art: Alloc concurrent mark sweep GC freed 3(72B) AllocSpace objects, 0(0B) LOS objects, 3% free, 494MB/510MB, paused 384us total 32.059ms 05-07 19:28:20.493 15034-16164/com.sender.hp.sender W/art: Throwing OutOfMemoryError "Failed to allocate a 48771084 byte allocation with 16777216 free bytes and 17MB until OOM"

I am now thinking that my images are eating up the memory, causing the slow image display and explaining why the text can still appear easily yet the images can't. I am going to try to compress the images before I add them to firebase.

Ben Henderson
  • 333
  • 1
  • 5
  • 15

1 Answers1

0

While it's impossible to know for certain with the information here exactly what is causing slowness, it's almost certain that adding event listeners without removing them when they're no longer needed will cause problems over time.

If you don't remove a listener when it's no longer needed, it will continue to receive changes that occur at the location where it's listening. In your case, that also means Picasso will continue to load images. Every time you add a listener like this without removing it, all the prior listeners will all still receive results and load images. I can only imagine that repeating this many times will quickly consume the available network bandwidth on a device with a mobile data connection.

Always be sure to remove listeners that you don't need any more.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441