是否可以阻止 NSURLRequest 缓存数据或在请求后删除缓存数据? [英] Is it possible to prevent an NSURLRequest from caching data or remove cached data following a request?
问题描述
在 iPhone 上,我使用 NSURLRequest 为一大块数据执行 HTTP 请求.对象分配高峰,我相应地分配数据.当我处理完数据后,我会相应地将其释放 - 但是仪器没有显示任何数据已被释放!
On iPhone, I perform a HTTP request using NSURLRequest for a chunk of data. Object allocation spikes and I assign the data accordingly. When I finish with the data, I free it up accordingly - however instruments doesn't show any data to have been freed!
我的理论是默认情况下会缓存 HTTP 请求,但是 - 我不希望我的 iPhone 应用缓存这些数据.
My theory is that by default HTTP requests are cached, however - I don't want my iPhone app to cache this data.
有没有办法在请求后清除此缓存或首先防止任何数据被缓存?
Is there a way to clear this cache after a request or prevent any data from being cached in the first place?
我已经尝试使用记录的所有缓存策略,如下所示:
I've tried using all the cache policies documented a little like below:
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
theRequest.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
但似乎没有什么可以释放内存!
but nothing seems to free up the memory!
推荐答案
通常创建这样的请求会更容易
Usually it's easier to create the request like this
NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:60.0];
然后创建连接
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request
delegate:self];
并在委托上实现 connection:willCacheResponse: 方法.只需返回 nil 即可.
and implement the connection:willCacheResponse: method on the delegate. Just returning nil should do it.
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
return nil;
}
这篇关于是否可以阻止 NSURLRequest 缓存数据或在请求后删除缓存数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!