I have an app in which I have to download a large number of files, from 400 to 900 files that are about 1GB total.
Which is the best approach to accomplish this?
NSURLSession
and all task enqueued in it? NSURLSession
and enqueue tasks by packages (10 by 10 for example)? NSURLSession
with different queues?Actually I have a NSURLSession
within all task (one per file) enqueued, but sometimes I get a Lost connection to background transfer service
.
Here is my code:
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;
});
}
}
When a user requests a background download, iOS spawns a separate process that does only this — downloads the file in the background. This process sends status notifications to the parent process. The parent process must conform to NSURLSessionTaskDelegate protocol to be able to receive notifications.
Dreams do come true. Well, at least if wildest imagination is limited to download large files on your iPhone or iPad using Safari. Well, in iOS 13 and iPadOS 13 you can actually do that. Safari now has a built-in download manager where it lets you download files straight to the local storage (or iCloud Drive).
All replies. Check in Settings>iTunes & App Store>Automatic Downloads>Updates>On. If that is set to "On", any apps that require updates are being downloaded in the background.
One NSURLSession - because you only want to handle session based things just once (auth for example).
One NSOperationQueue - with multiple operations running at the same time. (See property operationCount). It might be a little tricky implementing a NSOperation for the first time, but I'm sure this would be your best bet. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSOperationQueue_class/index.html
http://nshipster.com/nsoperation/
Oh and by the way, this is a highly object oriented approach, which is always nice =)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With