RGM-79FP GM Striker RGM-79FP GM Striker - 2 months ago 30
Swift Question

Sending to uithread from findinbackground

I just wanted to know if there was a way to send something to the ui thread after doing something in the background.

qUserActivity.findObjectsInBackground(block: {(objects, error) in
if error != nil{
print(error)
}else {
total = (objects?.count)!
total = 1

}
//Update the ui thread here?
})

Answer

You can do this:

swift 2.2

dispatch_async(dispatch_get_main_queue(), { 
            //your ui code here 
        }) 

swift 3.0

DispatchQueue.main.async(execute: { 
            // your ui code here
        })

So in your code:

qUserActivity.findObjectsInBackground(block: {(objects, error) in
    if error != nil{
        print(error)
    }else {
        total = (objects?.count)!
        total = 1


        dispatch_async(dispatch_get_main_queue(), { 
           self.myLabel.text = "something" 
        }) 
    }
})