Sunday, April 24, 2016

How to update Volley cache?

Leave a Comment

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!

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment