2

Hi I have an wired issues stopping my development. I am working on the application similar to facebook having posts, comments likes and chat functionalities. In each functionality I need to load the user's profile picture. I can able to load the profile pictures when I was trying to login for the first time using glide libary. But when users changes the profile picture server is returning the same url but different image. How to invalidate or load image when it was updated and what are the best ways to handle this scenario?

Your answers are valuable to me. Thanks in advance.

Sai's Stack
  • 1,345
  • 2
  • 16
  • 29

5 Answers5

1

like this:

Glide.with(MainActivity.this)
                .load("URL")
                .asBitmap().diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .placeholder(R.drawable.man_default)
                .into(imgUserImage);
Divyesh Patel
  • 2,576
  • 2
  • 15
  • 30
1

For Picasso you can use both MemoryPolicy and NetworkPolicy

Picasso  
    .with(context)
    .load(url)
    .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
    .networkPolicy(NetworkPolicy.NO_CACHE)
    .into(imageViewFromNetwork);

refer this its well explained here!

MadScientist
  • 2,134
  • 14
  • 27
0

I think disabling cache strategy will be helpful for you as your url is same so

DiskCacheStrategy.NONE
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
0

From Here

difference between the enum parameters for the .diskCacheStrategy() method:

DiskCacheStrategy.NONE caches nothing, as discussed

DiskCacheStrategy.SOURCE caches only the original full-resolution image. In our example above that would be the 1000x1000 pixel one

DiskCacheStrategy.RESULT caches only the final image, after reducing the resolution (and possibly transformations) (default behavior)

DiskCacheStrategy.ALL caches all versions of the image

try using:

Glide.with(context)
     .load(url)
     .placeholder(R.drawable.ic_profile)
     .diskCacheStrategy(DiskCacheStrategy.NONE)
     .skipMemoryCache(true)
     .into(ivUserProfilePhoto);

here is a relevant Question

Community
  • 1
  • 1
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • Thanks for the quick answer but when we are doing ".diskCacheStrategy(DiskCacheStrategy.NONE) .skipMemoryCache(true) " to image view, every time it loads from the server right? In that case, performance of the app will decreases. – Sai's Stack Mar 27 '17 at 06:31
  • depends...it will affect only the profile picture image loading...not the other. also you can check condition is the image updated then apply `diskCacheStrategy(DiskCacheStrategy.NONE)` otherwise use the caching – rafsanahmad007 Mar 27 '17 at 06:34
0

I am glad to give you suggestion over it.

First, you should remember every profile has unique URL and it should be immutable on the server.

These issue is more sort of on server side because you never know on client side when other users changed their profile pictures but server knows.

Probable solution we have done for it

  1. Whenever any user changes in profile picture we saving these picture on server with his timestamp and it attached to it's URL. eg : http://image.pngtimestamp=1336t78387

  2. So, now you have different URL for the user profile you don't have to change it on client side.

  3. Only thing you have to take care for it is how you will get these updated URL for the specific user.We are requesting for URL to server whenever we chat with other user or see his profile. so every time we get the latest pic.

Even you want to improve performance for it you can request for updated images URL at the start of your application when user launch app so server can return you all the images after given timestamp.

Hope these helps you.

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147