我知道这个问题已经被报告了很多次,但几乎所有的答案都与非 ARC 项目有关,而且我已经被困了好几天了。我看过很多论坛和文章。我希望我能在这里找到一些帮助。
我使用异步 NSURLConnection 请求来下载图片,使用 IOS 5,使用 ARC,如下(代码灵感来自《学习 IPAd 编程》一书):
-(void) dowloadImageAtURL:(NSURL *)URL
{
if (URL)
{
self.image = nil;
self.receivedData = [[NSMutableData alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request // Building asynchronous connection
delegate:self
startImmediately:NO]; // This is the key to not run the connection synchronous
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; // We use the current loop but it will still be asynchronous
// because of the mode added to the current loop
[connection start];
request = nil;
}
}
#pragma marks - NSURLConnection delegate Methods
/*
Called when the web server responds to the request.
When the method is called, the receivedData property is reset with a length of zero, clearing any previously stored data.
This ensures that only the data received from the final request is captured (see NSURLConnectionDelegate reference).
*/
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self.receivedData setLength:0];
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
sharedCache = nil;
connection = nil;
}
/*
Called when data is received from the network. The method can be called a multiple times during a single request.
The data is appended to the data already stored in the receivedData property.
*/
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.receivedData appendData:data];
}
/*
Called after the request has completed all data and all data has been received. This method converts
the data stored to a UIImage object.
*/
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
self.image = [UIImage imageWithData:self.receivedData];
self.receivedData = nil;
connection = nil;
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
sharedCache = nil;
// Here we post a notification to the observer
[[NSNotificationCenter defaultCenter] postNotificationName:@"com.perfectmemory.famille.imageupdated" object:self];
}
/*
Called if an error is detected at any time during the download process.
*/
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
self.receivedData = nil;
connection = nil;
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
sharedCache = nil;
// Here we post a notification to the observer
[[NSNotificationCenter defaultCenter] postNotificationName:@"com.perfectmemory.famille.imageupdated" object:self];
}
运行此代码时,下载运行良好,但我可以在 XCode 工具中看到泄漏
- URLConnectionLoader::LoaderConnectionEventQueue
- URLConnection::scheduleWithRunLoop
- CFURL响应
URLConnectionInstanceData
我已使用以下帮助尝试解决问题,例如清除缓存,但没有成功:
- NSURLConnection 泄漏——为什么?
- http://www.friendlydeveloper.com/2010/04/successfully-working-around-the-infamous-nsurlconnection-leak/
- NSURLConnection 泄漏?
我还尝试使用以下方法释放每个相关委托方法中的连接:
connection = nil
但仍然没有成功。我真的不明白发生了什么。我找不到未发布的对象。请你帮助我好吗 ?这很令人沮丧,因为我猜这个异步下载请求很常见。
提前非常感谢。