Android的web视图的ClearCache很慢 [英] Android Webview's ClearCache is very slow

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

问题描述

我使用WebViews在一个Android应用程序,我需要prevent的WebViews从缓存中。

不幸的是,好像这个看似简单的目标几乎是不可能实现的。我已经使出的解决方案是在onPageFinished事件执行webview.clearCache(真),使得缓存一个页面加载每次清除。有一些问题...

我已经注意到,作为高速缓存增长成为消费的clearCache方法来执行很花时间。有时,如果执行clearCache,然后切换到不同的活动,包含不同的web视图,即web视图不会加载几秒钟,因为它仍然在等待了previous clearCache操作完成。

更糟糕的是后续调用clearCache的执行时间似乎并没有降低后缓存已被清除。如果调用clearCache需要3秒内完成,然后我立即打电话给clearCache第二次,那么我会希望clearCache第二次调用几乎立即完成。但是,这不是我经历;我遇到了来clearCache第二个电话仍然需要约3秒。

任何人都经历呢?请问有什么办法可以提高性能?等待2-3秒钟一个的WebView加载(从本地文件系统)是可怕的。

编辑:

下面是实际清除缓存我最好的选择。它或多或少的作品,但是这有点片状,我不是100%满意的(写在单C#):

 公共类NoCacheWebClient:WebViewClient
{
    字符串previous;

    公众覆盖无效OnPageStarted(web视图查看,字符串URL,Android.Graphics.Bitmap图标)
    {
        base.OnPageStarted(查看,网址,网站图标);

        如果(!string.Equals(previous,网址))
        {
            previous =网址;
            view.Reload(); //重新加载一次忽略缓存
        }
        其他
        {
            previous = NULL;
        }
    }
}
 

解决方案

1)尝试使用<一个href="http://developer.android.com/reference/android/webkit/WebSettings.html#setAppCacheEnabled%28boolean%29"相对=nofollow> setAppCacheEnabled 和<一href="http://developer.android.com/reference/android/webkit/WebSettings.html#setAppCacheMaxSize%28long%29"相对=nofollow> setAppCacheMaxSize 限制缓存大小非常少,较低的缓存大小将导致更快的清理工作。

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

2)如果您不需要缓存的数据,然后简单地设置的 setCacheMode(WebSettings.LOAD_NO_CACHE); ,该装置 不使用高速缓存,负载从网络,即使数据缓存。

在短,根本不理会缓存数据,Android将照顾它。

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

注:这是仅适用于Android的API 8 +

 地图&LT;字符串,字符串&GT; noCacheHeaders =新的HashMap&LT;字符串,字符串&GT;(2);
    noCacheHeaders.put(杂,无缓存);
    noCacheHeaders.put(缓存控制,无缓存);
    view.loadUrl(URL,noCacheHeaders);
 

4)清除所有的时间,只要页面加载完成缓存。 这样的事情在 WebViewClient

  @覆盖
公共无效onPageFinished(web视图查看,字符串URL){
    super.onPageFinished(查看,网址);
    view.clearCache(真正的);
}
 

5)你可以尝试删除整个缓存的数据库一次。

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

这可能给快一点的结果,希望如此。

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

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...

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.

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.

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.

EDIT:

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) Try using setAppCacheEnabled and setAppCacheMaxSize to limit the cache size to very little , lower cache size will result in faster cleanup.

Ex: wv.getSettings().setAppCacheMaxSize(1);

OR

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.

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

OR

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

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

OR

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

OR

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的web视图的ClearCache很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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