2016-08-04 50 views
0

這是我的代碼。我想在傳遞數據和向數據庫添加數據時單擊按鈕時顯示一個指示符。我該如何解決它?加載指示器無法正常工作

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

回答

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. 

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

    }) 
    }) 

}