zyra / ionic-image-loader

Ionic 2+ Component that loads images in a background thread and caches them for later use
MIT License
436 stars 137 forks source link

Clearing cache not working #277

Open Bart1909 opened 4 years ago

Bart1909 commented 4 years ago

Hi,

I'm trying to clear the cache under ionic 4 in iOS.

My code looks like this:

const filesBefore = await this.file.listDir(this.file.cacheDirectory, 'image-loader-cache');
console.log('Files before:', filesBefore);
await this.imageLoader.clearCache();
const filesAfter = await this.file.listDir(this.file.cacheDirectory, 'image-loader-cache');
console.log('Files after:', filesAfter);

In the console it looks like this:

Files before: [Array] (306)
Files after: [Array] (306)
pcsantana commented 4 years ago

Hi @Bart1909 ! Did you find a solution? Thanks!

Bart1909 commented 4 years ago

Hi @pcsantana, no, not really. I'm doing a "workaround" with adding a query parameter to my urls, when changing the image on the backend. E.g. the url is https://mydomain.com/public/images/img1.jpg?timestamp=1584461500 When I change the image, i change the timestamp to current time. So the url is different and the image will be downloaded again. The disadvantage is, that the old image will retain on the device. But due to the fact, that my images do not change so often, this will be fine at the moment until the problem will be fixed.

Best wishes

pcsantana commented 4 years ago

Thank you @Bart1909 To solve the problem of delete the old image, I did a method to remove the cached file. It works! But while I doesn't restart the app, the image not change (dont know why). So, to change it instantly I ended up doing the same approach as you, adding a query parameter to load again.

If anyone wants, to remove the cached file:

async removeCacheFile(myImagePath: string): Promise<void> {
    try {
        const cachePath: string = await this.imageLoader.getImagePath(myImagePath);
        const filename: string = cachePath.substr(cachePath.lastIndexOf('/') + 1);
        const exists: boolean = await this.file.checkFile(this.file.cacheDirectory + "image-loader-cache/", filename);
        if (exists) {
            await this.file.removeFile(this.file.cacheDirectory + "image-loader-cache/", filename);
        }
    } catch (error) {
        console.error(error);
    }
}
gaurav-chandra commented 4 years ago

thanks @pcsantana for your code. This works.

elvisgraho commented 4 years ago

Will there be a fix?

Fieel commented 4 years ago

@pcsantana 's code is not a solution because you're clearing the cache of a single file. Here we're pointing out that the method used to clear ALL cache is not working anymore.

@Bart1909 how did you even manage to run the code? I get a "Property 'clearCache' does not exist on type 'IonicImageLoader'.", the method to clear all the cache data doesn't even exist anymore, what's happening?