Android Webview 的 ClearCache 非常慢 [英] Android Webview's ClearCache is very slow

查看:57
本文介绍了Android Webview 的 ClearCache 非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android 应用中使用 WebViews,我需要防止 WebViews 缓存.

I am using WebViews in an Android app, and I need to prevent the WebViews from caching.

不幸的是,这个看似简单的目标似乎几乎不可能实现.我使用的解决方案是在 onPageFinished 事件中执行 webview.clearCache(true) 以便每次加载页面时清除缓存.有一些问题...

Unfortunately it seems like this seemingly simple goal is nearly impossible to achieve. The solution I have resorted to use is to execute webview.clearCache(true) in the onPageFinished event so that the cache is cleared each time a page is loaded. There are some issues...

我注意到随着缓存的增长,执行 clearCache 方法变得非常耗时.有时,如果您执行 clearCache 然后切换到包含不同 webview 的不同 Activity,该 webview 将不会加载几秒钟,因为它仍在等待之前的 clearCache 操作完成.

I have noticed that as the cache grows it becomes very time consuming for the clearCache method to execute. Sometimes if you execute clearCache and then switch to a different Activity that contains different webview, that webview will not load for a few seconds because it is still waiting on the previous clearCache operation to finish.

更糟糕的是,在清除缓存后,后续调用 clearCache 的执行时间似乎并没有减少.如果对 clearCache 的调用需要 3 秒才能完成,然后我立即第二次调用 clearCache,那么我希望对 clearCache 的第二次调用几乎立即完成.但这不是我所经历的.我发现第二次调用 clearCache 仍然需要大约 3 秒.

What's worse is that execution time of subsequent calls to clearCache does not seem to decrease after the cache has been already cleared. If the call to clearCache takes 3 seconds to complete and then I immediately call clearCache a second time, then I would expect the second call to clearCache to complete almost immediately. But that is not what I'm experiencing; I'm experiencing that the second call to clearCache still take approximately 3 seconds.

有没有其他人遇到过这种情况?有什么办法可以提高性能吗?等待 2-3 秒让 webview 加载(从本地文件系统)是可怕的.

Has anyone else experienced this? Is there any way to improve performance? Waiting 2-3 seconds for a webview to load (from the local filesystem) is horrible.

这是我实际清除缓存的最佳替代方法.它或多或少有效,但有点不稳定,我对它不是 100% 满意(用 Mono c# 编写):

Here is my best alternative to actually clearing the cache. It more or less works but it's sort of flaky and I'm not 100% happy with it (written in Mono c#):

public class NoCacheWebClient : WebViewClient
{
    string previous;

    public override void OnPageStarted(WebView view, string url, Android.Graphics.Bitmap favicon)
    {
        base.OnPageStarted(view, url, favicon);

        if (!string.Equals(previous, url))
        {
            previous = url;
            view.Reload(); //re-load once to ignore cache
        }
        else
        {
            previous = null;
        }
    }
}

推荐答案

1) 尝试使用 setAppCacheEnabledsetAppCacheMaxSize 将缓存大小限制为很小,较小的缓存大小将导致更快的清理.

1) Try using setAppCacheEnabled and setAppCacheMaxSize to limit the cache size to very little , lower cache size will result in faster cleanup.

例如:wv.getSettings().setAppCacheMaxSize(1);

2) 如果您不需要缓存数据,那么只需设置 setCacheMode(WebSettings.LOAD_NO_CACHE); ,这意味着不要使用缓存,从网络加载",即使数据已缓存.

2) If you don't need the cached data then simply set setCacheMode(WebSettings.LOAD_NO_CACHE); , which means "Don't use the cache, load from the network", even though data is cached.

简而言之,只需忽略缓存的数据,android 会处理它.

In-short, simply ignore the cached data, android will take care of it.

3) 你也可以试试下面的无缓存代码,

3) you can also try the below code for no-caching,

注意:这仅适用于 Android API 8+

Note: this is only available for Android API 8+

    Map<String, String> noCacheHeaders = new HashMap<String, String>(2);
    noCacheHeaders.put("Pragma", "no-cache");
    noCacheHeaders.put("Cache-Control", "no-cache");
    view.loadUrl(url, noCacheHeaders);

4) 每次页面加载完成时清除缓存.WebViewClient 中的类似内容.

4) Clear the cache every-time whenever page load finishes. Something like this in the WebViewClient.

@Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);
    view.clearCache(true);
}

5) 您可以尝试一次删除整个缓存数据库.

5) You can try deleting whole cached database at once.

    context.deleteDatabase("webview.db");
    context.deleteDatabase("webviewCache.db");

这可能会得到更快的结果,希望如此.

This might give a bit faster result, hope so.

这篇关于Android Webview 的 ClearCache 非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆