I am loading data using code AFNetworking
let manager = AFHTTPRequestOperationManager()
manager.requestSerializer.cachePolicy = NSURLRequestCachePolicy.ReturnCacheDataElseLoad
manager.responseSerializer = AFHTTPResponseSerializer()
let testUrl = "https://www.google.com/maps/vt/pb=!1m8!3m7!1m2!1u2816!2u5888!2m2!1u1536!2u512!3i6!2m3!1e0!2sm!3i333!2m20!1e2!2spsm!4m2!1sgid!2sWejnA6yw2AIgHrbvnAUOAg!4m2!1ssp!2s1!8m11!13m9!2sa!15b1!18m5!2b1!3b0!4b1!5b0!6b0!19b1!19u12!3m2!2sen!5e1105!4e4!11m2!1e2!2b1&authuser=0"
manager.GET(testUrl,
parameters: nil,
success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in
print("afnetworking GET success1")
},
failure: { (operation: AFHTTPRequestOperation?,error: NSError!) in
print("afnetworking GET 1 - Error: " + error.localizedDescription)
})
When data is in cache, its giving me data from cache...its fine,
but how can I
1)- Set cache policy, to cache only for 1 hour
2)- When server haven't Caching headers its not caching, cache the data for an hour too for this case also.
You can solve it with setDataTaskWillCacheResponseBlock by changing response's header fields. You need
Expires
andExpires
fields.A sample in Objective C:
For the guys from
Swift
religion, I wroteAFHTTPSessionManager
extension with method which callsself.setDataTaskWillCacheResponseBlock:
fromAFNetworking 3.0
and modifiesCache-Control
response header value:Now you have very simple usage: