How to change label text with delay and in another thread?

0

As the question correctly says, I need to change the text of 4 different labels without the view being left without being able to process other processes and also between these changes of text I need a delay of at least 1 second. I already try to do it in an async block but from there I can not modify UI surely as a precaution measure of race condition .

Add: For now I have this code but I still can not change sleep and change the title of the button to the second.

    func startPings() {                      

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), {
                for boton in self.botones {
                    //print("Durmió")
                    dispatch_async(dispatch_get_main_queue(), {
                        boton.setTitle("<->", forState: .Normal)
                    })
                }
            })
        }
    
asked by MatiEzelQ 24.05.2016 в 22:43
source

2 answers

1

You can use the following extension:

extension NSObject {

    func delay(seconds: Double, fire:() -> ()) {

        let delay = seconds * Double(NSEC_PER_SEC)
        let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
        dispatch_after(time, dispatch_get_main_queue(), fire)

    }

}

Afterwards, you can use it anywhere in your code as follows:

self.delay(1.0, fire: {

    for boton in self.botones {
        boton.setTitle("<->", forState: .Normal)
    }

})
    
answered by 25.05.2016 в 09:03
0
func startPings() {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
        for boton in self.botones {
            dispatch_sync(dispatch_get_main_queue(), {
                boton.setTitle("<.>", forState: .Normal)
            })
            sleep(2)
            dispatch_sync(dispatch_get_main_queue(), {
                boton.setTitle("", forState: .Normal)
            })


        }
    })
}

Code that walked.

    
answered by 25.05.2016 в 17:13