title: How to perform multiple asynchronous requests starting one after another
body: <p>I have a set of requests that are performed asynchronous. However, each next request should only start when the previous request is finished (due to data dependency).  </p>
  
  <p>Since all requests should just be finished in the right order, <code>DispatchGroup()</code> seems of no use. </p>
  
  <p>I currently implemented <code>DispatchSemaphore()</code>, but I feel that this is not the best solution since I want to assure that all requests are performed in the background. </p>
  
  <pre><code>let semaphore = DispatchSemaphore(value: requests.count)
  
  for request in requests {
      apiManager().performAsyncRequest(request, failure: { error in
          print(error); semaphore.signal()
          }) { print(“request finished successful”) 
          // Next request should be performed now
          semaphore.signal()
      }
  }
  semaphore.wait()
  </code></pre>
  
  <p>Is there a better way to perform this? </p>
  
  <p><strong>Note</strong>: based on implementation of one of the answers below I encountered that <code>apiManager()</code> is not thread safe (due to the use of a Realm database).</p>
  
  <p>To keep this question clear and consider the answers in a thread safety manner with a thread safe definition of <code>performAsyncRequest</code>:</p>
  
  <pre><code>public func performAsyncRequest(_ requestNumber: Int, success: @escaping (Int) -&gt; Void)-&gt;Void {
      DispatchQueue(label: "performRequest").async {
          usleep(useconds_t(1000-requestNumber*200))
          print("Request #\(requestNumber) starts")
          success(requestNumber)
      }
  }
  </code></pre>
  
  <p><em>Solution with <code>DispatchSemaphore</code></em></p>
  
  <pre><code>        let semaphore = DispatchSemaphore(value: 1)
          DispatchQueue(label: "requests").async {
              for requestNumber in 0..&lt;4 {
                  semaphore.wait()
                  performAsyncRequest(requestNumber) { requestNumber in
                          print("Request #\(requestNumber) finished")
                          semaphore.signal()
                  }
              }
          }
  </code></pre>
  
  <p>With the expected output:</p>
  
  <pre><code>Request #0 starts
  Request #0 finished
  Request #1 starts
  Request #1 finished
  Request #2 starts
  Request #2 finished
  Request #3 starts
  Request #3 finished
  </code></pre>
  
  <p><em>Unsuccessful try with <code>Operation</code></em></p>
  
  <pre><code>    var operations = [Operation]()
  
      for requestNumber in 0..&lt;4 {
          let operation = BlockOperation(block: {
              performAsyncRequest(requestNumber) { requestNumber in
                  DispatchQueue.main.sync {
                      print("Request #\(requestNumber) finished")
                  }
              }
          })
  
          if operations.count &gt; 0 {
              operation.addDependency(operations.last!)
          }
          operations.append(operation)
      }
  
      let operationQueue = OperationQueue.main
      operationQueue.addOperations(operations, waitUntilFinished: false)
  </code></pre>
  
  <p>With incorrect output:</p>
  
  <pre><code>Request #0 starts
  Request #1 starts
  Request #2 starts
  Request #3 starts
  Request #0 finished
  Request #3 finished
  Request #2 finished
  Request #1 finished
  </code></pre>
  
  <p>My feeling is that it should also be possible to get this working with <code>Operation</code>, but I don't know whether it would be better than using <code>DispatchSemaphore</code>. </p>