Lankybrit
Lankybrit

Reputation: 51

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.

Upvotes: 5

Views: 5804

Answers (1)

Salman Ghumsani
Salman Ghumsani

Reputation: 3657

Call it in Dispatch Queue:

Swift 3.x

DispatchQueue.main.async {
    updateYourUI()
}

Swift 2.x

dispatch_async(dispatch_get_main_queue()) {
   updateYourUI()       
}

Upvotes: 4

Related Questions