i begin to developp an iphone application and i need some advice about the NSURLSession and about how to manage properly the download and parsing of my data.
i just finish to correct a bug for the download of my data in a nsurlsession, but regarding how difficult i find to understand these asynchronus request, i think my solution is not really good... also the download bug appear with 2 different solution of download, wich make me think that i forgot to do something...
In, my project i download different xml files (and some zip files with pictures sometime) that i need to parse before to display their informations. these informations can change quickly so i want to download them again if i load my page again. i was looking for an easy way to manage all the downloads in the same way, like that i wouldn't have to re write a lot of code.
i found this project first.
With that , i just had to use that code to manage the downloads :
NSString *downloadUrl = @"https://www.url.com";
NSURL *location = [NSURL URLWithString:downloadUrl];
// DownloadManager is my version of the CTSessionOperation of the github project
DownloadManager *operation = [DownloadManager new];
operation.downloadUrl = downloadUrl;
operation.completionAction = ^(NSURL *xmlUrl, BOOL success){
dispatch_async(dispatch_get_main_queue(), ^{
if (success){
regions = [[TeamChoiceManager sharedManager] parseRegions:[NSData dataWithContentsOfURL:location]];
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:Nil waitUntilDone:YES];
}
});
};
operation.isBackground = YES;
[operation enqueueOperation];
this code works perfectly the first time i download. but if i try to launch again the download, the download is not lauched (so no error, just, this code download once and that's all).
i correct this bug by modifying the metod (NSURLSession *)session
in CTSessionOperation
/ DownloadManager
. i put in comment the "dispatch_once" to make it works, but i don't think that it's the good solution...
i tried an other solution wich led to the same bug. i manage the download with this code :
NSString *regionsUrl= @"url";
NSURLSessionConfiguration *sessionConfig =
[NSURLSessionConfiguration defaultSessionConfiguration];
// My solution to the bug
/*NSURLSessionConfiguration *backgroundConfiguration = [NSURLSessionConfiguration
backgroundSessionConfiguration:[NSString stringWithFormat:@"com.captech.mysupersession.BackgroundSession%d",numBGSession]]; */
// numBGSession++; this is a static NSInteger
NSURLSession *session =
[NSURLSession sessionWithConfiguration:backgroundConfiguration
delegate:teamChoiceDetailViewController
delegateQueue:nil];
NSURLSessionDownloadTask *sessDLTask =
[session downloadTaskWithURL:[NSURL URLWithString:regionsUrl]];
[sessDLTask resume];
and in the delegate :
-(void)URLSession:(NSURLSession *)session
downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location
{
dispatch_async(dispatch_get_main_queue(), ^{
self.regions = [[TeamChoiceManager sharedManager] parseRegions:[NSData dataWithContentsOfURL:location]];
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:Nil waitUntilDone:YES];
});
}
with this solution, i avoid the bug by creating a custom NSURLSessionConfiguration
every time i try to download.
so here we go. i'm quite confuse with this 2 solutions. i don't know if they are proper way to manage the download, i don't think i correct the bug correctly, and i must have missed something with the logic of NSURLSession
.
do you have any advice to improve these solutions or do you see one wich is much better than the other?