swift - 带有 NsurlConnection 的 UIViewActivityIndi​​cator 不会快速停止动画

标签 swift ios8

我在单击按钮时调用 callDailyQuotes 方法。 activityIndi​​cator 在从服务器下载数据时开始动画,但随后 activityIndi​​cator 停止动画。

相关代码如下:

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

发生了什么使 activityIndi​​cator 停止动画?

最佳答案

你必须在主线程中执行 self.activityIndi​​cator.stopAnimating() 调用,所以我的建议是将它移动到你已经添加的 dispatch_async但留空:

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
}

关于swift - 带有 NsurlConnection 的 UIViewActivityIndi​​cator 不会快速停止动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28938894/

相关文章:

ios - 在 ios swift3 中使用自定义相机捕获视频

ios 8 : When clicked inside UITextview, 键盘没有弹出,但在 ios 7 中运行良好

iOS 8 - 禁用 iCloud 照片共享事件

ios - 有没有一种方法可以拖动 Segues 来使 Storyboard 看起来更漂亮?

ios - 从 swift api 调用获取 JSON 响应

ios - 给定一个实时流的深层链接,我如何在我的 iOS 应用程序中显示这个实时流?

ios - Xcode 6 中的自定义字体 - Swift 不起作用

swift - 无法在 Swift 中通过 MapKit 绘制折线

ios - Swift:值未加载到文本字段

ios - 为什么 CGImageCreateWithImageInRect 使用带点的 CGRect 然后使用像素?