ionic-image-loader icon indicating copy to clipboard operation
ionic-image-loader copied to clipboard

Clearing cache not working

Open Bart1909 opened this issue 5 years ago • 6 comments

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)

Bart1909 avatar Feb 06 '20 16:02 Bart1909

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

pcsantana avatar Mar 16 '20 14:03 pcsantana

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

Bart1909 avatar Mar 17 '20 16:03 Bart1909

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);
    }
}

pcsantana avatar Mar 17 '20 16:03 pcsantana

thanks @pcsantana for your code. This works.

gaurav-chandra avatar Mar 24 '20 07:03 gaurav-chandra

Will there be a fix?

elvisgraho avatar May 06 '20 09:05 elvisgraho

@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?

Fieel avatar Jun 24 '20 11:06 Fieel