I have a strange problem when I try to invalidate an NSURLSession
instance.
The code is quite simple: I have a View Controller, two buttons (start: and stop:), and a text field for the url.
A simple extract of the code:
- (IBAction)start:(id)sender {
NSURLSessionConfiguration *conf = [NSURLSessionConfiguration backgroundSessionConfiguration:@"conf"];
self.session = [NSURLSession sessionWithConfiguration:conf delegate:self delegateQueue:nil];
NSURLSessionDownloadTask *task = [self.session downloadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url.text]]];
[task resume];
}
- (IBAction)cancel:(id)sender {
[self.session invalidateAndCancel];
}
or, if you prefer, the whole project: Link
Now, try to download a file ( http://download.thinkbroadband.com/1GB.zip ).
Since I want this download to continue in the background, I'm using a background session.
The session starts correctly and the download continue in the background, but if I try to cancel it (sending invalidateAndCancel
) I have a bad access.
Profiling with Zombie enabled give this zombie object: _NSCFBackgroundDownloadTask
.
So, if I retain the NSURLSessionDownloadTask
(using a strong property to store it) the bad access doesn't happen.
But, AFAIK, NSURLSession should retain it's tasks itself, so I would like to understand what's wrong with my code (maybe I'm missing something in the docs?) or if I should file a bugreport.
Thanks