1

I asked a question about how to update Volley cache in Android yesterday and no one answered it, and I can not find any documentation about updating cache in Volley. I'm wondering if maybe I misunderstood the way cache could be updated or not.

How can I update Volley cache?

EDIT:

just for clarification, i bring my code below to explain my issue:

private void checkCache(String service_address,
                                   final int termId,
                                   int node_Id) {
    Cache cache = MyApplication.getInstance().getRequestQueue(true).getCache();
    Cache.Entry entry = cache.get(service_address);
    /*
    cache is full- maybe new maybe old
     */
    if (entry != null) {


        if(networkChecker.isConnected()) {
            Calendar calendar = Calendar.getInstance();
            long serverDate = MyApplication.getInstance().getRequestQueue(false).getCache().get(service_address).serverDate;
            if (Utility.getMinutesDifference(serverDate, calendar.getTimeInMillis()) >= 30) {
                MyApplication.getInstance().getRequestQueue(false).getCache().invalidate(service_address, true);

                //does this code referesh cache?how to referesh cache for new incomming data

            }
        }
        ShowOrUpdateBadger();
        pDialog.hide();
    }
    /*
    //cache is empty---- should get all news
     */
    else {
        /*
        can not fetch data-->close app
         */
        if (!networkChecker.isConnected()) {
            pDialog.cancel();
            Utility.ShowFaildMessage(MainActivity.this);
        }
        /*
        load from internet
         */
        else {
            GetonlineNewGalleryData(service_address, termId, true, node_Id);
        }
    }
}

I call above method in my application's opening in onCreate() in MainActivity. The question is, in below code, how can I update cache with new news?

if (entry != null) {
...
}

EDITE:

I update my code ,and my code is like below:

  private void checkCache(String service_address,
                                   final int termId,
                                   int node_Id) {

    Cache cache = MyApplication.getInstance().getRequestQueue().getCache();
    Cache.Entry entry = cache.get(service_address);

    if(networkChecker.isConnected())
    {
        if(entry!=null)
        {
            MyApplication.getInstance().getRequestQueue().getCache().invalidate(service_address,true);
            MyApplication.getInstance().getRequestQueue().getCache().remove(service_address);
        }
        GetonlineNewGalleryData(service_address, termId, true, node_Id);
    }
    else
    {
        if(entry==null)
        {
            pDialog.cancel();
            Utility.ShowFaildMessage(MainActivity.this);
        }
        else
        {
            ShowOrUpdateBadger();
            pDialog.hide();
        }


    }

Now the problem is , my cache data was 400 news and new data is 2 news after doing update cache my cache number is 802 news!

Community
  • 1
  • 1
MSepehr
  • 890
  • 2
  • 13
  • 36
  • 2
    There is an upvoted and accepted answer to your question... Anyway, if you want to make sure that everything is up to date just invalidate your cache every time you enter the app. http://stackoverflow.com/questions/24464610/how-to-clear-the-volley-cache-automatically – zgc7009 Apr 14 '16 at 14:03
  • @zgc7009 owww! the link was wrong .i edit it – MSepehr Apr 14 '16 at 14:23
  • Please read http://stackoverflow.com/questions/31897189/android-setup-volley-to-use-from-cache/32022946#32022946 or http://stackoverflow.com/questions/34064066/tell-volley-not-to-use-cached-data-but-to-initiate-a-new-request/34079943#34079943 or http://stackoverflow.com/questions/32415284/stop-volley-from-sending-cache-headers/32420318#32420318 to see if they can help or not – BNK Apr 14 '16 at 14:24
  • @BNK i used the first link in my app to caching , and the second link is the exact problem i have! this is my problem which he mentioned:"I would not like to delete the complete cache. I'd only like to tell Volley when to initiate a brand new request to the API. " I will check this out.. – MSepehr Apr 14 '16 at 14:31
  • @zgc7009 invalidate is not the solution , because i do not want to refresh all my cache , i just want to add new data to existing cache – MSepehr Apr 14 '16 at 14:36
  • Did you even read what invalidate does in the post I pasted? `Invalidate means you are just marking the data as invalid. So volley will check with the server whether the data is still valid. The full expire determines whether to use the data before volley has validated it with the server.` You are telling the server not to delete the data, but to validate it against the server. – zgc7009 Apr 14 '16 at 14:45
  • @zgc7009 i used your code ,it did not refresh the cache:( – MSepehr Apr 14 '16 at 14:53
  • It isn't supposed to refresh the cache, it is supposed to invalidate the cache to determine if the call you are making corresponds to valid data in your cache or not. – zgc7009 Apr 14 '16 at 14:54
  • @zgc7009 so how to refresh the cache and add new data? – MSepehr Apr 14 '16 at 15:04
  • You clear the cache... Do realize that you can clear cache by key... – zgc7009 Apr 14 '16 at 16:25
  • @zgc7009 i update my question. could you help me? – MSepehr Apr 15 '16 at 03:36
  • @BNK i edited my question,please look, is this link: http://stackoverflow.com/questions/34064066/tell-volley-not-to-use-cached-data-but-to-initiate-a-new-request/34079943#34079943 the solution of my question? – MSepehr Apr 15 '16 at 03:37
  • @serenei have you tried your code above, does it work? If not, you can read http://stackoverflow.com/questions/24464610/how-to-clear-the-volley-cache-automatically – BNK Apr 15 '16 at 04:20
  • @BNK i used your link in my code and the question is "How can I add new data to cache" . i know ,`invalidate` does not referesh the cache ,right? so after invalidate cache how can i add new data to it? – MSepehr Apr 15 '16 at 04:23
  • @BNK and yes i tried code above , it get cached data again – MSepehr Apr 15 '16 at 04:26
  • @BNK do i use this code `getMinutesDifference(serverDate, ca...` in the right place in my code? – MSepehr Apr 15 '16 at 04:28
  • To get new data, I often do `private void removeCacheEntry(String url) { RequestQueue requestQueue = VolleySingleton.getInstance(mContext).getRequestQueue(); if (requestQueue != null) { Cache cache = requestQueue.getCache(); if (cache != null) { // cache.invalidate(url, true); cache.remove(url); } } }` – BNK Apr 15 '16 at 04:43
  • I use your code and it did not get new data ,it just clear my cache, i'm getting crazy..:( – MSepehr Apr 15 '16 at 05:30
  • after clear the cache, send request to get new data – BNK Apr 15 '16 at 05:47
  • @BNK could you please check my Edit 2? – MSepehr Apr 15 '16 at 12:09
  • Have you debugged yet? `MyApplication.getInstance().getRequestQueue().getCache().invalidate(service_address,true); MyApplication.getInstance().getRequestQueue().getCache().remove(service_address);` called or not? – BNK Apr 15 '16 at 13:48
  • @BNK yes! i debuged it and my code reach these lines – MSepehr Apr 15 '16 at 15:26
  • GetonlineNewGalleryData right after that did not get new data? – BNK Apr 15 '16 at 15:39
  • What is your question? "no complete documentation about volley" doesn't sound like a question. – rds Apr 21 '16 at 21:00
  • @rds: May be he wanted to means that. just update the cache. Suppose at first call he received 400 data and keep those in cache. After 30 minutes he request server to get the new data in last 30 minutes. Suppose server gives him new 10 data. Now he wants to update his cache, so that the new cache contains 410 data(old 400 and new 10). This is also my question. How can I do this in Volley? Thanks... – Zenith Aug 18 '18 at 09:20
  • @MaSepehr: Have you got your solution? If yes, I need your help :) – Zenith Aug 18 '18 at 09:51
  • @ Zenith sorry, it was for a long time ago! i don't remember actually – MSepehr Aug 19 '18 at 21:53

0 Answers0