0

I am doing a analysis process on image. As I run through all pixels, I want to have a feedback about the progress. Here is my code:

       for _ in 0 ..< height {
            for _ in 0 ..< width {
                analyzeImage(currentPixel)
                currentPixel = currentPixel.successor()
            }
            updateProgress()
        }

func updateProgress() {
    self.fractionalProgress += self.fractionalRatio
    if (Int(self.fractionalProgress / self.fractionalRatio) % 20) == 0 {
        self.progressBar.progress = self.fractionalProgress
        print(self.progressBar.progress)
    }
}

I called updateProgress inside a nested for loop. The printed result is updating, but the bar is not changed visually. It is only updated at the end. Thanks for any help in advanced !!!!

1 Answers1

0

UIKit is single threaded and is only meant to be run from the main thread. If you've got a loop performing work on the main thread, then UIKit won't update until after your code has finished running and your method has returned.

In order to show progress, you need to perform the work on a background thread and update UIKit on the main thread. Read Concurrency Programming Guide.

Jim
  • 72,985
  • 14
  • 101
  • 108