0

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()

            }
        }

    //}
}
jasonh
  • 1
  • 2
  • I don't understand. Why are you using an `async` dispatch call, when you're going to use a `DispatchGroup` like a `DispatchSemaphore` to ensure that `buttonPush` push waits for the `async` closure to finish. Why not just use a plain `sync` call? – Alexander Nov 20 '17 at 03:24
  • Thanks for the reply. Honestly, I wasn't real sure to use async vs. sync. Sync definitely makes more sense for what I want the code to do (based on the info found here on this other thread): https://stackoverflow.com/questions/44324595/difference-between-dispatchqueue-main-async-and-dispatchqueue-main-sync I'm assuming I need to do something like the second example in this thread: https://stackoverflow.com/questions/19179358/concurrent-vs-serial-queues-in-gcd/35810608#35810608 – jasonh Nov 21 '17 at 03:42
  • I’ve been fighting with this for the last few weeks on/off. I’m still not sure how to get this working. I’m trying to run the timer() for 5 rounds. However, when the while loop runs it does not wait for the timer() to finish. Is dispatch group the way to go? While the timer is running I want the user to still be able to interact with the UI. – jasonh Dec 12 '17 at 03:34
  • Looks like you'll want to use multi-threading. You should read more about Dispatch, DispatchQueues, sync/async, etc. before proceeding – Alexander Dec 12 '17 at 04:20
  • I’ll check this out. Thank you. – jasonh Dec 12 '17 at 04:37
  • Ok, I'm able to make this work with each "button click". However, when I add the while loop it appears to go into an infinite loop. – jasonh Dec 14 '17 at 04:16
  • K. The more I dig into GCD and multitreading the more I get confused. I understand the difference between async and sync and I definitely seem to want to use sync as I want my tasks to finish in the same(serial) order all the time. The confusion lies in the syntax. I referred to this: https://stackoverflow.com/questions/19179358/concurrent-vs-serial-queues-in-gcd/35810608#35810608 as my main reference and want to use “doLongSyncTaskInSerialQueue” type function. – jasonh Dec 18 '17 at 05:38
  • however I’m not sure how to implement a while loop into this. The user inputs the number of rounds and the while loop runs the timer for each round until it hits 0. I would think I want this to all be running in the main thread as it updates the UI constantly?? – jasonh Dec 18 '17 at 05:38
  • Let me go on and clarify something. The runclock() function in counting down a timer on the UI. I’m trying to run “count down” and the other functions for “n” number of round. “n” is a user input. Is GCD still the right approach? I don’t want anything done in the background. Seems like threading is over kill. I just want the while loop to countdown rounds and run the functions. Still a noob to all of this. – jasonh Dec 20 '17 at 05:44
  • I'm busy, but ping me later, I'll try to take a look at this – Alexander Dec 20 '17 at 06:00
  • Cool man. Thanks! Let me know if you need anymore info. – jasonh Dec 21 '17 at 02:35
  • setTimer() takes user input and assigns to a var. runtimer() is a Timer that uses a selector countdown(). countdown() simply counts the time down -= 1. resetTimer() resets the time back to the user input. I reset it because I wanted to run multiple rounds with the same user inputted time round after round...I'm going to be in and out for the next few hours. thanks again for your help and time. – jasonh Dec 21 '17 at 02:38
  • Hey Alexander. I think I figured this out. Thanks for the help. Cheers. – jasonh Dec 27 '17 at 04:27

0 Answers0