0

I have been using AFNetworking 2.0. Cache works well if the app is in foreground, or came from background. But when I kill the app and reopen it, cache is cleared. Is there a way to make it persistent? Thanks

Oscar J. Irun
  • 455
  • 5
  • 17

1 Answers1

0

You can cache from disk using the NSURLRequestReturnCacheDataElseLoad cache policy:

Objective-c

NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]
                                              cachePolicy:NSURLRequestReturnCacheDataElseLoad    
                                          timeoutInterval:60];

[imageView setImageWithURLRequest:imageRequest
                 placeholderImage:[UIImage imageNamed:@"placeholder"]
                          success:nil
                          failure:nil];

Swift

let imageRequest = NSURLRequest(URL: NSURL.URLWithString(imageUrl), 
                        cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, 
                    timeoutInterval: 60)

imageView.setImageWithURLRequest(imageRequest, 
                             placeholderImage: UIImage(named: "placeholder"), 
                                      success: nil, 
                                      failure: nil)

Source: here

Rayron Victor
  • 2,398
  • 1
  • 25
  • 25