0

I am downloading multiple files using NSUrlSession in my iOS app. I want to download only one file at a time. But it is downloading multiple files at 1 time. Please suggest a way to download files one at a time.

I tried this property :

sessionConfiguration.HTTPMaximumConnectionsPerHost = 1

But it also download multiple files if they are hosted on different servers.Please suggest a way to do this

princ___y
  • 1,089
  • 1
  • 9
  • 27
ashu
  • 11
  • 1
  • 5
  • NSUrlSession downloads files in the background and provides a completion handler when finished. You could download the next in the completion handler of the previous. However, why do you want to download one at a time. That will only be slower. – Michael Feb 02 '16 at 05:57

1 Answers1

0

You have 2 options:

  1. sync: Daisy chain your requests from the completionHandler, triggering a new NSURLSession resume upon the completion or failure of the previous one.

  2. async: Each operation must complete before the next one can start, and waits until completion signals using a semaphore. The thread performing the scheduling of each NSURLSession resume will wait for that operation to complete. See https://stackoverflow.com/a/21205992/218152

sync/async clarification

Neither of these options are suggesting to run your operations on the main thread: sync/async is merely referring to the background thread responsible for the scheduling.

Community
  • 1
  • 1
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179