Downloading a large number of files in the background in iOS

I have an application in which I need to upload a large number of files, from 400 to 900 files, which are about 1 GB.

What is the best approach to achieve this?

  • One NSURLSession and all tasks set in it?
  • One NSURLSession and the start of the packet queue (for example, 10 by 10)?
  • Multiple NSURLSession with different queues?

Actually, I have an NSURLSession inside the whole task (one per file), 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; }); } } 
+5
source share
1 answer

One NSURLSession - because you want to process only session things only once (e.g. auth).

One NSOperationQueue - performing multiple operations simultaneously. (See propertyCount property). It may be a little difficult to implement NSOperation for the first time, but I'm sure this will 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 very object-oriented approach, which is always good =)

+1
source

Source: https://habr.com/ru/post/1207406/


All Articles