nsurlcache

NSURLCache cachedresponseforrequest no data


why the responseCache is nil? i'd run this post and really get the responseObject from cache. How can i get the responseCache?

manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

manager.requestSerializer.cachePolicy=NSURLRequestReturnCacheDataElseLoad;

[manager POST:URL parameters:paramdic progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

    NSData * data=[NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:nil];

    NSURLCache * cache=[NSURLCache sharedURLCache];

    NSCachedURLResponse * responseCache=[cache cachedResponseForRequest:task.originalRequest];

    NSCachedURLResponse * response=[[NSCachedURLResponse alloc]initWithResponse:task.response data:data userInfo:nil storagePolicy:NSURLCacheStorageAllowed];


    [cache storeCachedResponse:response forRequest:task.originalRequest];




} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"%@",error);
}];

Solution

  • There are three reasons it is nil at that point:

    This is not necessarily a complete set of reasons. :-)

    The cache is intended to reduce network traffic. You shouldn't generally consult it yourself. The normal lookup path used by NSURLSession et al performs checks for certain protocol caching policies (e.g. response expiration) that would not be performed by merely asking the cache if it has a response for a particular key.

    If you need a general mechanism for storing a single response for later use by your app (rather than keeping it in memory), you should do so in your own internal dictionary (or, if the response is large, by using a download task and moving the file into a temporary folder in your app's sandbox that you purge on every launch).