装载指示灯无法正常工作

时间:2016-08-04 12:02:58

标签: swift uiactivityindicatorview

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

@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()
            })
        }
    })
}

2 个答案:

答案 0 :(得分:0)

喜欢以下......

   @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()

    })
    })

}

答案 1 :(得分:0)

您的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.

    })
}