title: using a swift loop that waits on functions to complete
body: <p>I'm new and trying to learn and code my first app. A simple app to get started...So I'm trying to make a loop that will perform a series of functions completely for a certain number of times (rounds). I've figured out the body of code and I'm trying to implement DispatchGroup() to wait on the functions to perform:</p>
  
  <pre><code>@IBAction func buttonPush(_ sender: Any) {
  
      let group = DispatchGroup()
  
      while rounds &gt;= 0 {
  
          group.enter()
  
          DispatchQueue.main.async {
  
              if self.isGameStarted == 0 {
  
                  self.setTimer()
                  self.resetVariables()
                  self.runClock()
  
                  self.roundLabel.text = String(self.self.rounds)
                  group.leave()
  
              } else if self.isGameStarted &gt;= 1{
  
                  self.self.isGameStarted = 0
                  self.timer.invalidate()
                  group.leave()
  
              }
          }
  
          group.notify(queue: .main) {
              self.rounds -= 1
          }
  
      }
  }
  </code></pre>
  
  <p>Here was the original body pre DispatchGroup(). The issue was that the while loop was completing before the functions could run, I think (best guess). I'm using the latest version of swift as of 11/19/17.</p>
  
  <pre><code>@IBAction func buttonPush(_ sender: Any) {
  
      while rounds &gt;= 0 {
  
              if isGameStarted == 0 {
  
                  setTimer()
                  resetVariables()
                  runClock()
                  roundLabel.text = String(rounds)
  
              } else if isGameStarted &gt;= 1{
  
                  isGameStarted = 0
                  timer.invalidate()
                  rounds -= 1
  
              }
          }
      }
  </code></pre>
  
  <p>12/13/17 edit. This works for each button press and while loop commented out. However, with while loop uncommented it goes into infinite loop...</p>
  
  <pre><code>     @IBAction func buttonPush(_ sender: Any) {
  
      //while rounds &gt;= 0 {
  
         if isGameStarted == 0 {
  
                  setTimer()
                  resetVariables()
                  runClock()
                  roundLabel.text = String(rounds)
                  DispatchQueue.main.asyncAfter(deadline: .now() + Double(timeRoundTot){
                  self.rounds -= 1
  
              } else if isGameStarted &gt;= 1{
  
                  isGameStarted = 0
                  timer.invalidate()
  
              }
          }
  
      //}
  }
  </code></pre>