作者:心在想念-小凡 | 来源:互联网 | 2023-05-18 04:01
HereisthesamplefunctionicallwhenineedtoclearcacheandmakeanewcalltoURL这是我需要清除缓存并对
Here is the sample function i call when i need to clear cache and make a new call to URL
这是我需要清除缓存并对URL进行新调用时调用的示例函数
- (void)clearDataFromNSURLCache:(NSString *)urlString
{
NSURL *requestUrl = [NSURL URLWithString:urlString];
NSURLRequest *dataUrlRequest = [NSURLRequest requestWithURL: requestUrl];
NSURLCache * cache =[NSURLCache sharedURLCache];
NSCachedURLResponse* cacheRespOnse=[cache cachedResponseForRequest:dataUrlRequest];
if (cacheResponse) {
NSString* dataStr = [NSString stringWithUTF8String:[[cacheResponse data] bytes]];
NSLog(@"data str r= %@",dataStr);
NSLog(@"url str r= %@",[[[cacheResponse response] URL] absoluteString]);
[cache storeCachedResponse:nil forRequest:dataUrlRequest];
[NSURLCache setSharedURLCache:cache];
}
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:dataUrlRequest];
//Check if the respnase data has been removed/deleted from cache
NSURLRequest *finalRequestUrlRequest = [NSURLRequest requestWithURL:requestUrl];
NSURLCache * finalCache =[NSURLCache sharedURLCache];
NSCachedURLResponse* finalcacheRespOnse=[finalCache cachedResponseForRequest:finalRequestUrlRequest];
if (finalcacheResponse) {
//Should not enter here
NSString* finaldataStr = [NSString stringWithUTF8String:[[finalcacheResponse data] bytes]];
NSLog(@"data str r= %@",finaldataStr);
NSLog(@"url str r= %@",[[[cacheResponse response] URL] absoluteString]);
}
}
In iOS 6/7 the response is deleted successfully for the requestURL, but in iOS 8 it never gets deleted. I have searched but could not find reason why this should not work in iOS8.
在iOS 6/7中,requestURL的响应被成功删除,但在iOS 8中它永远不会被删除。我已搜索但无法找到原因,这在iOS8中不起作用。
Any help will be appreciated…..
任何帮助将不胜感激…..
2 个解决方案