title: Async tasks execution with dependency
body: <p><strong>Situation:</strong> </p>
  
  <p>I have 2 tasks says <strong>T1</strong> &amp; <strong>T2</strong> in async background mode. <strong>T2</strong> depends on <strong>T1</strong> and have <strong>successBlock</strong> which is executes after the completion of the both tasks <strong>T1</strong> &amp; <strong>T2</strong>.</p>
  
  <p>Quick diagram is below for better understanding.</p>
  
  <blockquote>
    <p><a href="https://i.stack.imgur.com/GooJe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GooJe.png" alt="enter image description here"></a></p>
  </blockquote>
  
  <p><strong>Edit:</strong></p>
  
  <p>To better understanding the tasks, you can assume T1 and T2 are the API calls which always be going to execute in async mode. I need some output data from T1 to hit T2 API. After the completion of the both tasks I need to update UI.</p>
  
  <hr>
  
  <p>To accomplish this scenario, I have added my first async work in <strong>T1</strong> and second work in <strong>T2</strong> and dependency of <strong>T2</strong> to <strong>T1</strong> and successblock have dependency on both tasks.</p>
  
  <p><strong>Code Work</strong></p>
  
  <ol>
  <li><p>My Tasks</p>
  
  <pre><code>class TaskManager {
  
      static let shared = TaskManager()
  
      func task1Call(complete: @escaping ()-&gt;()) {
          DispatchQueue.global(qos: .background).async {
              for i in 0...10 {
                  print("~~&gt; Task 1 Executing ..", i)
                  sleep(1)
              }
              complete()
          }
      }
  
      func task2Call(complete: @escaping ()-&gt;()) {
          DispatchQueue.global(qos: .background).async {
              for i in 0...10 {
                  print("==&gt; Task 2 Executing ..", i)
                  sleep(1)
              }
              complete()
          }
      }
  }
  </code></pre></li>
  <li><p>Execute Tasks</p>
  
  <pre><code>class Execution {
  
      // Managing tasks with OperationQueue
      func executeTaskWithOperation()  {
  
          let t1 = BlockOperation {
              TaskManager.shared.task1Call {
                  print("Task 1 Completed")
              }
          }
  
          let t2 = BlockOperation {
              TaskManager.shared.task2Call {
                  print("Task 2 Completed")
              }
          }
  
          let successBlock = BlockOperation {
              print("Tasks Completed")
          }
  
          let oper = OperationQueue()
  
          t2.addDependency(t1)
          successBlock.addDependency(t2)
          successBlock.addDependency(t1)
  
          oper.addOperations([t1, t2, successBlock], waitUntilFinished: true)
  
      }
  }
  
  let e = Execution()
  e.executeTaskWithOperation()
  </code></pre></li>
  </ol>
  
  <hr>
  
  <p><strong>Issue:</strong></p>
  
  <p>Both tasks are executing parallelly and <strong>successBlock</strong> executes before the completion of task 1 and task 2.</p>
  
  <p><strong>Console Output:</strong></p>
  
  <pre><code>==&gt; Task 2 Executing .. 0
  Tasks Completed
  ~~&gt; Task 1 Executing .. 0
  ~~&gt; Task 1 Executing .. 1
  ==&gt; Task 2 Executing .. 1
  ==&gt; Task 2 Executing .. 2
  ~~&gt; Task 1 Executing .. 2
  ==&gt; Task 2 Executing .. 3
  ~~&gt; Task 1 Executing .. 3
  ==&gt; Task 2 Executing .. 4
  ~~&gt; Task 1 Executing .. 4
  ==&gt; Task 2 Executing .. 5
  ~~&gt; Task 1 Executing .. 5
  ==&gt; Task 2 Executing .. 6
  ~~&gt; Task 1 Executing .. 6
  ==&gt; Task 2 Executing .. 7
  ~~&gt; Task 1 Executing .. 7
  ==&gt; Task 2 Executing .. 8
  ~~&gt; Task 1 Executing .. 8
  ==&gt; Task 2 Executing .. 9
  ~~&gt; Task 1 Executing .. 9
  ~~&gt; Task 1 Executing .. 10
  ==&gt; Task 2 Executing .. 10
  Task 1 Completed
  Task 2 Completed
  </code></pre>
  
  <p>I unable to figure out what wrong I am doing, even same code work fines when I use sync mode instead of async.</p>