user3823935
user3823935

Reputation: 891

UIViewActivityIndicator with NsurlConnection does not stop animation in swift

I am calling callDailyQuotes methods on button click. The activityIndicator starts animating as data is downloaded from the server, but then the activityIndicator stops animating.

Here is the relevant code:

self.activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
self.activityIndicator.startAnimating()
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
   self.callDailyQuotes()
   dispatch_async(dispatch_get_main_queue(), { () -> Void in     
            })
        })

func callDailyQuotes(){
//calling nsurlconnection synchronously
 self.activityIndicator.stopAnimating()
}

What's happening that makes the activityIndicator stop animating?

Upvotes: 1

Views: 592

Answers (1)

Antonio
Antonio

Reputation: 72750

You have to execute the self.activityIndicator.stopAnimating() call in the main thread, so my suggestion is to move it in the dispatch_async you've already added but left empty:

self.activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
        self.activityIndicator.startAnimating()
        let qualityOfServiceClass = QOS_CLASS_BACKGROUND
        let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
        dispatch_async(backgroundQueue, {
            self.callDailyQuotes()
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.activityIndicator.stopAnimating()
            })
        })

func callDailyQuotes(){
//calling nsurlconnection synchronously
}

Upvotes: 3

Related Questions