2

I'm using LRUCache to cache images that I retrieve from a server. I'm requesting them from a RecyclerView but till all the images are cached the LRUCache instance returns size 0.

Here's the Code

public void buildCache() {
        for (Feed feed : mFeeds) {
            List<User> users = new ArrayList<>();
            users.add(feed.getAuthor());
            try {
                if (feed.getPostType() == Feed.PostType.Collab) {
                    for (User receiver : feed.getUsers())
                        users.add(receiver);
                    getImages(users);
                    Log.d(LOG_TAG, "Cache size: " + imagesLruCache.size());
                } else if (feed.getPostType() == Feed.PostType.Search){
                    String receiverUsername = feed.getReceiver().getUsername();
                    getImages(users);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        mCacheBuildingInterface.OnCacheBuilt();
        dataSetChanged();
}

What should I do to fix the problem ?
Thx to everyone

Andrea Tulimiero
  • 17,693
  • 5
  • 21
  • 28
  • A) Why not use [`LruCache`](http://developer.android.com/reference/android/util/LruCache.html)? A `SoftReference` does not know wether it was recently used or not so the garbage collector does a terrible job at collecting the "right" thing (It's typically just collecting all soft references at once, thus wiping your cache). SoftReferences are no good cache. B) if your cache returns only `null` maybe it'a point A) – zapl Sep 01 '15 at 09:02
  • C) SoftReferenceCache#toString() clears the cache (`it.remove()`) D) You're using picasso which has it's own (working) cache implementation. I'd honestly just get rid of all your manual caching attempts. – zapl Sep 01 '15 at 09:17
  • Thx for your advice @zapl . I've just updated the code, and now I'm using the LRUCache that's working like a charm. The only problem is still the first images that are not loading. – Andrea Tulimiero Sep 01 '15 at 09:36
  • @zapl in the end I succed to use the Picasso caching service and it's working like a charm, thx for you advice again – Andrea Tulimiero Oct 08 '15 at 08:23

0 Answers0