1

I have a program that download a video from a url using NSURLSession, but i'm not able to do multiple download at the same time. How can i do it? How can i manage multiple simultaneous download?

    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration    defaultSessionConfiguration];

    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];

    NSURLSessionDownloadTask *getVideo = [session downloadTaskWithURL:fileURL
                                                    completionHandler:^(NSURL *location,
                                                                        NSURLResponse *response,
                                                                        NSError *error) {
                                                        // 2
                                                        receivedData = [NSData dataWithContentsOfURL:location];
                                                        dispatch_async(dispatch_get_main_queue(), ^{
                                                            // do stuff with image
                                                             NSLog(@"%s receiveData:%d",__FUNCTION__,[receivedData length]);
                                                        });
                                                    }];
    [getVideo resume];
Adrian P
  • 6,479
  • 4
  • 38
  • 55

1 Answers1

0

From the code you have provided above you are not using any of the properties of NSURLSessionConfiguration class that would enable better download performance.

First of all I would look suggest using your own delegate queue. If you do not provide a queue then the session creates a serial operation queue for all delegate and completion handler calls see the "Creating a Session" section of the NSURLSession Class Reference document for more detail. You can look at the following properties of NSOperationQueue to help improve performance;

qualityOfService
maxConcurrentOperationCount

Next I would look at NSURLSessionConfiguration properties that may help.

HTTPMaximumConnectionsPerHost
HTTPShouldUsePipelining

Finally you should review the section "Life Cycle of a URL Session with Custom Delegates". You should confirm whether your using the delegate methods of NSURLSessionTaskDelegate and NSURLSessionDownloadTaskDelegate or just the completion handler.

You need to put more time into configuring NSURLSession to support the work you want to do.

Peter Hornsby
  • 4,208
  • 1
  • 25
  • 44