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:
@IBAction func buttonPush(_ sender: Any) {
let group = DispatchGroup()
while rounds >= 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 >= 1{
self.self.isGameStarted = 0
self.timer.invalidate()
group.leave()
}
}
group.notify(queue: .main) {
self.rounds -= 1
}
}
}
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.
@IBAction func buttonPush(_ sender: Any) {
while rounds >= 0 {
if isGameStarted == 0 {
setTimer()
resetVariables()
runClock()
roundLabel.text = String(rounds)
} else if isGameStarted >= 1{
isGameStarted = 0
timer.invalidate()
rounds -= 1
}
}
}
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...
@IBAction func buttonPush(_ sender: Any) {
//while rounds >= 0 {
if isGameStarted == 0 {
setTimer()
resetVariables()
runClock()
roundLabel.text = String(rounds)
DispatchQueue.main.asyncAfter(deadline: .now() + Double(timeRoundTot){
self.rounds -= 1
} else if isGameStarted >= 1{
isGameStarted = 0
timer.invalidate()
}
}
//}
}