我有一个应用程序,我必须在其中下载大量文件,从 400 到 900 个文件,总共大约 1GB。
实现这一目标的最佳方法是什么?
- 一项
NSURLSession
和所有任务都排在其中? - 一个
NSURLSession
并按包排列任务(例如 10 x 10)? - 多个
NSURLSession
不同的队列?
实际上,我有一个NSURLSession
在所有任务中(每个文件一个)排队,但有时我得到一个Lost connection to background transfer service
.
这是我的代码:
if([[UIDevice currentDevice] isMultitaskingSupported])
{
__block UIBackgroundTaskIdentifier bgTask;
UIApplication *application = [UIApplication sharedApplication];
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *uuidString;
CFUUIDRef uuid = CFUUIDCreate(nil);
uuidString = CFBridgingRelease(CFUUIDCreateString(nil, uuid));
CFRelease(uuid);
// }
NSURLSessionConfiguration *sessionConfiguration;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
{
sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.fiveflamesmobile.bakgroundDownload"];
}
else
{
sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.fiveflamesmobile.bakgroundDownload"];
}
sessionConfiguration.HTTPMaximumConnectionsPerHost = 5;
sessionConfiguration.sessionSendsLaunchEvents = YES;
sessionConfiguration.discretionary = YES;
sessionConfiguration.timeoutIntervalForResource = 0; //NO timeout
sessionConfiguration.timeoutIntervalForRequest = 0; //No timeout
sessionConfiguration.networkServiceType = NSURLNetworkServiceTypeBackground;
self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration
delegate:self
delegateQueue:nil];
NSLog(@"##### ------- Sesion created succesfully");
// [self batchDownloading];
for (id<FFDownloadFileProtocol> file in self.selectedCatalogProducto.downloadInfo.arrayFiles)
{
[self startDownloadFile:file];
}
NSLog(@"##### ------- Download tasks created successfully ------");
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
}