Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clearing cache not working #277

Open
Bart1909 opened this issue Feb 6, 2020 · 6 comments
Open

Clearing cache not working #277

Bart1909 opened this issue Feb 6, 2020 · 6 comments

Comments

@Bart1909
Copy link

Bart1909 commented Feb 6, 2020

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
Copy link
Contributor

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

@Bart1909
Copy link
Author

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
Copy link
Contributor

pcsantana commented Mar 17, 2020

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
Copy link

thanks @pcsantana for your code. This works.

@elvisgraho
Copy link

Will there be a fix?

@Fieel
Copy link

Fieel commented Jun 24, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants