0

I want to store the response fetched from a request in NSUrlCache.How. How can this be done?

-(void)sendRequestForImage

{  

    NSCachedURLResponse * cachedResponse = [Cache cachedResponseForRequest:requestImage];

    if(!cachedResponse.data)

    {
     NSString*imageStr=[NetworkManger getImage:requestImage]; 
        _imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageStr]]];

    }

    else

    { 
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:cachedResponse.data options:NSJSONReadingAllowFragments error:nil];    


      }   
}

Now I am trying to remove the object for request requestImage and I am trying to do this as follows:

- (IBAction)clearImage:(id)sender {

    [Cache removeCachedResponseForRequest:requestImage];
   }

But this doesn't work. Why?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Prez
  • 227
  • 3
  • 12
  • Do you code in Swift or Objective-C? For Swift there is a great tutorial: http://nshipster.com/nsurlcache/ In Obj-C Apple provided also some good code samples: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/URLLoadingSystem/Concepts/CachePolicies.html Can you specify your problem or where you are stuck? – Lepidopteron May 16 '17 at 06:44
  • I code in objective c...I am stuck in the method remove cachedresponseforrequest.When I am trying to remove the data in cache related to particular request it doesn't work but when I removesallobject,it works nicely.Can you explain why? – Prez May 16 '17 at 15:08
  • Can you share some code and illustrate, what is working and what is not on a code basis? Makes it easier to explain :-) – Lepidopteron May 16 '17 at 15:09
  • @Lepidopteron...Sir,code added.Kindly check. – Prez May 16 '17 at 15:50

0 Answers0