'The progress view bar does not update/grow in a quiz app

When you give the correct answer in a multiple choice test, a question number, score points update/increase, but the progress bar does not, it remains the same, not changing.

Below is the code, which separates the width of the screen/view into equal parts depending on the number of questions. I found it earlier in YouTube, and it worked on the project/app shown there. But I cannot apply it in my case and I don’t know why. progressView.frame.size.width = (view.frame.size.width / CGFloat(questions.count)) * CGFloat(questionNumber)

I have some yellow alert notifications in main.storyboard, but I think it should not cause this problem.

@IBOutlet weak var questionCounterLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var progressView: UIProgressView!
@IBOutlet private var questionNumberLabel: UILabel!
@IBOutlet private var questionNameLabel: UILabel!
@IBOutlet private var tableView: UITableView!


private func shuffleQuestions() {
  shuffledQuestions = questions.shuffled()
  questionNumber = 1
  score = 0
  showQuestion()
}

private func showQuestion() {
  questionCounterLabel.text = "\(questionNumber)/\(questions.count)"
  scoreLabel.text = "Score: \(score)"
  
  currentQuestion = shuffledQuestions[questionNumber - 1]
  questionNameLabel.text = currentQuestion?.text ?? ""
  
  questionNumberLabel.text = "Question \(questionNumber)"
  questionNumber += 1
  score += 1
  progressView.frame.size.width = (view.frame.size.width / CGFloat(questions.count)) * CGFloat(questionNumber)

  tableView.reloadData()
}

private func checkAnswer(for answer: Answer) {
  if answer.correct {
    if questionNumber > questions.count {
      scoreLabel.text = "Score: \(score)"
            let alert = UIAlertController(title: "Awesome",
                                          message: "End of Quiz. Do you want to start over?",
                                          preferredStyle: .alert)
            let restartAction = UIAlertAction(title: "Restart",
                                              style: .default,
                                              handler: { action in self.shuffleQuestions() } )
            alert.addAction(restartAction)
            present(alert, animated: true, completion: nil)
      return
    }
    showQuestion()
  } else {
    // wrong
    let alert = UIAlertController(title: "Wrong",
                                  message: "You failed",
                                  preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "Dismiss",
                                  style: .cancel,
                                  handler: nil))
    present(alert, animated: true)
  }
}


Solution 1:[1]

Changes to the UI should always occur on the main/foreground thread, in this case it's happening on a background queue. Use:

  DispatchQueue.main.async {
     self.progressView.frame.size.width = (view.frame.size.width / CGFloat(questions.count)) * CGFloat(questionNumber)
}

Update:

DispatchQueue.global(priority: .default).async {
   DispatchQueue.main.async {
      self.progressView.frame.size.width = (view.frame.size.width / CGFloat(questions.count)) * CGFloat(questionNumber)
   }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1