I'm making a very simple app to practice multi-threading using GCD. I initialized a UIActivityIndicatorView in my ViewController class like so:
class ViewController: UIViewController {
var myActivityIndicator:UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
In my viewDidLoad, I added it as a subView to my main view with the following code:
override func viewDidLoad()
{
super.viewDidLoad()
myActivityIndicator.center = view.center
view.addSubview(myActivityIndicator)
}
Then when my button is tapped, I have an IBAction that handles the event by printing a simple message after a little while. Here is the code for that:
@IBAction func doSomething(sender: AnyObject)
{
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0))
{
self.myActivityIndicator.startAnimating()
sleep(3)
print("This has been accessed on another thread")
dispatch_async(dispatch_get_main_queue())
{
self.myActivityIndicator.stopAnimating()
}
}
}
It seems as though self.myActivityIndicator.startAnimating() isn't getting called, but when I put that line of code in viewDidLoad, it works just fine.
What am I missing?