是否可以防止NSURLRequest在请求后缓存数据或删除缓存的数据? [英] Is it possible to prevent an NSURLRequest from caching data or remove cached data following a request?
问题描述
在iPhone上,我使用NSURLRequest对一大块数据执行HTTP请求。对象分配峰值和我相应地分配数据。当我完成数据,我释放它相应 - 但仪器不显示任何数据被释放!
我的理论是,默认HTTP请求缓存,但是 - 我不想我的iPhone应用程序缓存这些数据。
有没有办法在请求后清除此缓存,或阻止任何数据首先被缓存?
NSMutableURLRequest * theRequest = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:url]];
theRequest.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
但没有任何内容释放内存!
任何帮助将非常感谢。
谢谢!
Nick。
通常这样更容易创建请求
NSURLRequest * request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:60.0];
然后创建连接
NSURLConnection * conn = [NSURLConnection connectionWithRequest:request
delegate:self];
并在代理上实现连接:willCacheResponse:
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
return nil;
}
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!
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!
Any help here would be much appreciated.
Thanks! Nick.
Usually it's easier to create the request like this
NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:60.0];
Then create the connection
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request
delegate:self];
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屋!