Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating label takes too long (swift)

Tags:

xcode

ios

swift

I'm beginner swift developer. I'm stucked with this weather app.I'm downloading website data and then displaying in my label.

Unfortunately this whole process takes like 10 second to update my label.

This is probably not because of the network connection as the console is updated instantly.

Thanks for suggestions.

enter image description here

like image 371
selez Avatar asked Sep 24 '14 09:09

selez


2 Answers

What happens is that code is probably run on a secondary thread. Any UI changes you make should be made on the main thread. So try this:

dispatch_async(dispatch_get_main_queue()) {
    // update label
}

This should update your label instantly.

like image 95
Mihai Fratu Avatar answered Oct 13 '22 17:10

Mihai Fratu


Previously, we would choose the dispatch method (sync vs async) and then the queue we wanted to dispatch our task to. The updated GCD reverses this order - we first choose the queue and then apply a dispatch method.

Swift 3:

Now in Swift 3 the GCD library was updated like in the following way:

DispatchQueue.main.async(execute: {
    // UI Updates
})

I hope this help you.

like image 44
Victor Sigler Avatar answered Oct 13 '22 17:10

Victor Sigler