JakeWharton / DiskLruCache

Java implementation of a Disk-based LRU cache which specifically targets Android compatibility.
http://jakewharton.github.io/DiskLruCache
Apache License 2.0
5.79k stars 1.18k forks source link

Last DiskLruCache.get() in loop returns null on existing key #15

Closed Bdiang closed 12 years ago

Bdiang commented 12 years ago

Hi!

I'm loading images in ListView from DiskLruCache. ListAdapter.getView() method creates AsyncTask which loads image from DiskLruCache. And there is very strange behavior - DiskLruCache.get() returns null on last one ListView element in bunch of lazy loaded ListView elements.

To simplify situation imagine loop:

for (i...) { SnapShot value = DiskLruCache.get("key"+i); }

For the last "i" value is null. A checked that on UI thread also, but still same result.

If it's not a bug but my mistake maybe someone would help me with issue?

Thanks!

P.S. DDMS shows that cache entry exist. Clearing and filling it again brings same result. P.P.S. It does not depend on items count. It could be different entry from time to time, but always last in bunch of get() calls

JakeWharton commented 12 years ago

Can you try writing a failing test case for this behavior? I'll try to reproduce when I get time but a failing test case would more easily help in identifying the problem and surfacing a solution.

Bdiang commented 12 years ago

OK

Bdiang commented 12 years ago

I did more research and discovered that problem was in my code.

nemanja-kovacevic commented 11 years ago

@Bdiang Can you please tell was what was the bug in your code since I'm seeing the same behavior - last saved item is present on disk but I'm not getting it from cache...

Bdiang commented 11 years ago

Actually I don't know. I had function IsKeyExists where I checked snapshot existance but not getting it. And in the loop last one was always not exists (but it was). I just stop using that functuion)

nemanja-kovacevic commented 11 years ago

Thanks for your effort. Anyway, my problem was with android killing the app and journalWriter not beeing flushed so changes were lost... Its a known issue here as I've later discovered...