Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force UI Display to update in swift?

I'm trying to get the View Controller to update while I'm in a loop that calls a function that does tons of Label updates. But the update to the screen doesn't happen until I've finished the loop.

Is there a way to force a UI update before continuing? Something like forcing data to be written to a disk.

code for the button to start the loop is something like this:

        while (match.currentInnings == currentInnings)
        {
            playSingleBall()
            if (gameover == true)
            {
                return
            }
        }

After playSingleBall, I'd like to force a UI update before continuing.

Thanks.

like image 415
Lankybrit Avatar asked Oct 18 '22 10:10

Lankybrit


1 Answers

Call it in Dispatch Queue:

Swift 3.x

DispatchQueue.main.async {
    updateYourUI()
}

Swift 2.x

dispatch_async(dispatch_get_main_queue()) {
   updateYourUI()       
}
like image 99
Salman Ghumsani Avatar answered Nov 01 '22 11:11

Salman Ghumsani