I need to parse quite a lot of text and wanted to provide some feedback to the user while this is taking place.
The environment is Swift and while I do see some code in Obj-C ([self performSelector:@selector(.....)), it doesn't make a lot of sense. If I knew how to take that and embed it into a Swift approach, I would do.
I can summarise the problem with a small reproducible case that gives the same result. i.e. within a loop, increment a value, show the progress and go back until done. Obviously, the progress isn't going to be shown because iOS waits until the loop is done before refreshing the screen.
That does make sense, so I would like at various intervals to interrupt the processing (i.e. the loop) and refresh the progress bar before continuing with the processing.
My current code looks like this:
@IBAction func goButton(sender: UIButton) {
currentCounter = 0
target = textTarget.text.toInt()!
step = textStep.text.toInt()!
updateProgressBar()
while currentCounter < target {
currentCounter += step
updateProgressBar()
}
}
func updateProgressBar() {
var percentage = Float(currentCounter) / Float(target) * 100
progressPercentage.text = "\(percentage) %"
println("progress = \(percentage)")
progressBar.setProgress(percentage, animated: false)
}
I've seen the following:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
// do some task here...
}
dispatch_async(dispatch_get_main_queue()) {
// do another task here...
}
How might I use this approach (if it's relevant), and where would the processing versus the call to refresh come in if I did?
Jon