I\'m trying to show a spinner during a possibly lengthy action invoked by a button press, but I can\'t get it to show up. Here\'s my code:
class ViewController:
What you want is something like:
@IBAction func buttonPressed(sender: UIBarButtonItem) {
spinner.startAnimating()
let dispatchPriority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(dispatchPriority, 0)) {
NSThread.sleepForTimeInterval(5.0) //your task here instead of this line
dispatch_async(dispatch_get_main_queue(), {
self.spinner.stopAnimating()
})
}
}
This starts the spinner,then dispatches to a background thread, performs a task there (I'm assuming the task you are performing is one that can be performed on a background thread - i.e it doesn't involve UI stuff). Once the task is complete, the method dispatches back to the main UI thread, and stops the spinner.