dmytro-anokhin / url-image

AsyncImage before iOS 15. Lightweight, pure SwiftUI Image view, that displays an image downloaded from URL, with auxiliary views and local cache.
MIT License
1.11k stars 96 forks source link

Remove image cache with completion block #107

Closed pballart closed 3 years ago

pballart commented 3 years ago

Is your feature request related to a problem? Please describe. I'm trying to remove an image that is cached but since the progress happens in a background thread I don't know when it has completed.

Describe the solution you'd like It would be nice to have a completion block called when the process finish so I can refresh the UI and be sure that the image will be downloaded from network and the cache will be gone.

Describe alternatives you've considered Right now I'm using a DispatchQueue.main.asyncAfter of half a second before updating the UI to be sure the cached image is completely removed.

Additional context Add any other context or screenshots about the feature request here.

dmytro-anokhin commented 3 years ago

Hey, I need to think about the problem. At a glance adding a completion seems like a solution. On a second thought it doesn't fit SwiftUI declarative syntax. Also I would expect image to be deleted from cache immediately. So this is more of a race between the view and the cache. Anyways, thanks for reporting 👍

dmytro-anokhin commented 3 years ago

The version in master branch will delete images using barrier and this should guarantee that images are always deleted before being accessed. Can you please try it and see if it solves your case? I'm gonna do a bit more testing before making a release.

pballart commented 3 years ago

Hey @dmytro-anokhin thanks for taking a look. I just tested the master branch and I'm seeing same issue. to give a bit of context I have a view that has an URLImage and a button. When tapping the button I open the native UIImagePicker, select another image upload it to the server and save the url. Open saving, the view is updates because of a model change and the new image should appear. Something interesting that I noticed is that while using URLImageService.shared.removeImageWithURL(url) doesn't work if I use URLImageService.shared.removeAllCachedImages() there are no issues. I'm gonna spend some time looking at the implementation of both since removing all cached images does work perfectly.

pballart commented 3 years ago

Digging a bit deeper I could find that after removing the image from both the database and disk, once the view reloads and the URLImage tries to get it from cache it actually still exists in the database and return a correct file. It's super weird because using an identifier also fixes the problem, it just in the case of the URL that doesn't work 🤔

dmytro-anokhin commented 3 years ago

Can you provide a sample code?

dmytro-anokhin commented 3 years ago

From my side, this is how I'm testing it: https://github.com/dmytro-anokhin/url-image-demo/blob/main/URLImageDemo/View/Issues/Issue107View.swift

pballart commented 3 years ago

With your test code you would need to change the image of the url and then click "Load" and see if the new image appears. Anyway using the identifier works fine for me and I can't really spend more time looking into that right now.

Maybe we can close this issue for now but keep an eye in case someone else has similar issues. Thank you very much for looking into it 😃

dmytro-anokhin commented 3 years ago

Hey, in the example the service I use (https://picsum.photos/) will return a random image for each request. So URL doesn't change, images do. I'm closing the issue, but if you find something please do not hesitate to report.