2

I have a progress bar that should update after every step. the steps can be very high, let's say 50.000. When inputting this large number, the progress bar doesn't do anything when the iPhone is calculating. When the iPhone is done, the progress bar is instantly at 1.0 (max) progress. This is my code:

    for x in 1...timesToSimulate{
        var progress: Float = Float(x) / Float(timesToStimulate)
        progressBarView.setProgress(progress, animated: true)
}

The calculation isn't that hard, but I am doing something wrong but can not see what. You know what it is?

Tiago Mussi
  • 801
  • 3
  • 13
  • 20
Petravd1994
  • 893
  • 1
  • 8
  • 24
  • Possible duplicate of [Updating the UI Using Dispatch\_Async in Swift](http://stackoverflow.com/questions/26743367/updating-the-ui-using-dispatch-async-in-swift) – ebby94 Jan 14 '17 at 14:20
  • are you doing this on the main thread? – Jon Brooks Jan 14 '17 at 18:30
  • I don't know what thread, I never modified anything on threads. Just the normal thread i guess – Petravd1994 Jan 15 '17 at 12:49
  • Probably it is updating, but it happens instantly, so you may not see the change. If you want to update the progress bar every second, consider Using NSTimer. – Raunak Joneja Aug 06 '18 at 09:06

1 Answers1

-1

The reason is probably that all your calculation is done on the main tread. The UI is only updated after your function returns (at which point you have reached the maximum progress). If you want to update the progress bar step by step, you need to change the progress asynchronously.

For example:

    for x in 1...timesToSimulate{
        let progress: Float = Float(x) / Float(timesToSimulate)
        DispatchQueue.main.asyncAfter(deadline: .now() + Double(x) * 0.1) {
            self.progressBarView.setProgress(progress, animated: true)
        }
    }
silicon_valley
  • 2,639
  • 2
  • 16
  • 20
  • The problems remains exactly the same when changing the code to your answer :( – Petravd1994 Jan 14 '17 at 16:37
  • Are you sure you're not setting the progress anywhere else in your code? The code I posted works perfectly fine for me... – silicon_valley Jan 14 '17 at 16:39
  • It's strange, the code after the DispatchQueue won't execute. The progress bar doesn't do anything... I emptied my code to just your line of code. It won't execute. When I try to do print("??") the print won't show. However the function is certainly called. I don't know what to do. – Petravd1994 Jan 14 '17 at 16:50
  • No! Example: func executeRandomize(timesToStimulate: Int) { print("1") for x in 1...timesToStimulate{ print("2") let progress: Float = Float(x) / Float(timesToStimulate) DispatchQueue.main.asyncAfter(deadline: .now() + Double(x) * 0.1) { print("3") self.progressBarView.setProgress(progress, animated: true) } } } <--- It prints "1", it prints "2" the amount of timestostimulate. But it won't print "3" – Petravd1994 Jan 14 '17 at 18:30
  • This example still won't work if this code was called on the main thread. Try wrapping this whole block in a `DispatchQueue.global().async { ... }` call. – Jon Brooks Jan 14 '17 at 18:34
  • Well I got a lot of code in the debug when adding this. You mean adding this in the function right? Still the same result, only more debug code and its a lot slower now. – Petravd1994 Jan 15 '17 at 12:49