I have a problem with some GUI components in my iOS app when using NSURLSession delegates.
I have a UITableViewController which show different map areas to a user. The user can pick one of these areas and I will push a new UIViewController showing some details about this area. Inside the UIViewController the user can click a button to download this map area.
I use NSURLSession to download the map. I have my own method for setting up the session.
- (NSURLSession *)backgroundSession {
static NSURLSession *session = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.example.app"];
sessionConfiguration.HTTPMaximumConnectionsPerHost = 2;
session = [NSURLSession sessionWithConfiguration:sessionConfiguration
delegate:self
delegateQueue:nil];
});
return session;
}
And I start the download by
self.downloadTask = [self.session downloadTaskWithURL:[NSURL URLWithString:hikingMap.downloadSource]];
[downloadTask resume];
This works very fine on the first area I open from the UITableViewController.
Example:
1. Go to area A
2. Start downloading area A
However, my problem arises when I look at some areas first and then later go to another area and try to download this area.
Example:
1. Go to area A
2. Go back to UITableViewController
3. Go to area B
4. Start downloading area B
The actual download runs just fine. However, I make some changes to the GUI once the download starts. I hide some labels and show some UIProgressViews.
I do those changes in the delegate method for the download progress:
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
dispatch_async(dispatch_get_main_queue(), ^{
self.label.hidden = YES;
self.progressView.hidden = NO;
});
}
Those hidden changes is not reflected in my GUI. Basically all changes to the GUI I do outside of the delegate methods works just fine, but none of the changes I do inside the delegate methods works.
I do not understand how I can solve this problem. So any help would be great! Tell me if I need to add more code for you to understand what is going on.
UPDATE
I have now confirmed that the delegate method uses an old reference to the UIlabel. I test by changing the label text when opening area A and when I then go to area B and do the download I see that the label text in the delegate methods is the same as it was in area A.