加载指示器无法正常工作

Loading Indicator is not working correctly

这是我的代码。我想在传递数据和向数据库添加数据期间单击按钮时显示一个指示器。我该如何解决?

@IBAction func addWishList(sender: AnyObject) {
     self.loading.startAnimating()
        addMovie(showMovie!) // Database add method.
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in
        let controller : WishListViewController = segue.destinationViewController as! WishListViewController

        if segue.identifier == "segue" {
            controller.user = self.tmpUser //  data from Facebook to pass another controller
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.loading.stopAnimating()
            })
        }
    })
}

喜欢关注..

   @IBAction func addWishList(sender: AnyObject) {

         self.loading.startAnimating()
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in

            addMovie(showMovie!) // Database add method.

    dispatch_async(dispatch_get_main_queue(), { () -> Void in
    self.loading.stopAnimating()

    })
    })

}

您的 addMovie 阻塞主队列。您需要延迟一些时间才能将其添加到下一个 运行 循环。 尝试使用:

@IBAction func addWishList(sender: AnyObject) {
    self.loading.startAnimating()
    //This will add to next main run loop.
    let dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.0 * Double(NSEC_PER_SEC))) 
    dispatch_after(dispatchTime, dispatch_get_main_queue(), { 
            addMovie(showMovie!) // Database add method.

    })
}