ios - UIActivityIndi​​catorView 在异步任务期间不显示

标签 ios swift asynchronous uiactivityindicatorview swift2

我正在 iOS 中开发一个应用程序,其中我需要开始旋转 UIActivityIndi​​catorView,将图像上传到服务器,并在上传完成后停止旋转事件指示器。

我目前正在使用 XCode 7 Beta,并在 iOS 模拟器上作为 iPhone 6 和 iPhone 5 测试该应用程序。我的问题是,事件指示器不会在文件上传后立即结束,而是会持续几个(约 28 秒) ) 之后。我应该在哪里调用电话才能结束?

我有一个 @IBOutlet 函数附加到用于启动进程的按钮,该函数包含 startAnimating() 函数,并调用一个包含对 uploadImage 的调用的dispatch_async 方法,该方法包含信号、等待和 stopAnimating () 函数。

请注意

let semaphore = dispatch_semaphore_create(0)
let priority = DISPATCH_QUEUE_PRIORITY_HIGH

是在我的类的顶部定义的。

@IBAction func uploadButton(sender: AnyObject) {

        self.activityIndicatorView.startAnimating()
        dispatch_async(dispatch_get_global_queue(priority, 0)) {

         self.uploadImage(self.myImageView.image!)

        }  // end dispatch_async

    }   // works with startAnimating() and stopAnimating() in async but not with uploadImage() in async


    func uploadImage(image: UIImage) {

        let request = self.createRequest(image)

        let session : NSURLSession = NSURLSession.sharedSession()

        let task : NSURLSessionTask = session.dataTaskWithRequest(request, completionHandler: {
            (data, response, error) in

            if error != nil {
                print(error!.description)
            } else {
                let httpResponse: NSHTTPURLResponse = response as! NSHTTPURLResponse

                if httpResponse.statusCode != 200 {
                    print(httpResponse.description)
                } else {

                    print("Success! Status code == 200.")
                    dispatch_semaphore_signal(self.semaphore)
                    }
            }
        })! // end dataTaskWithResult

        task.resume()

        dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER)
        self.activityIndicatorView.stopAnimating()
    } // end uploadImage

这只是我的代码的一个版本,我已经通过几种不同的方式移动了一些内容。我已经尝试过:

@IBAction func uploadButton(sender: AnyObject) {

        self.activityIndicatorView.startAnimating()
        dispatch_async(dispatch_get_global_queue(priority, 0)) {

         self.uploadImage(self.myImageView.image!)
         dispatch_semaphore_signal(self.semaphore)
        }  // end dispatch_async
    dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER)
    self.activityIndicatorView.stopAnimating()
    }

还有几种其他方法来移动我的代码,以尝试让事件指示器在图像上传期间显示,然后立即退出。在某些情况下,旋转器在程序执行期间根本不会出现。我读过this发布和this问题并已将我的dispatch_semaphore_wait和stopAnimating()迁移到uploadImage()方法来规避这个问题,但在UIActivityIndi​​catorView文档中找不到有关UI更新的足够信息来了解更新它的任何其他方式,尽管我相信这可能是问题的核心。

我所需要的只是让微调器在上传过程开始(dataTaskWithRequest)之前启动,并在成功或失败后结束。我做错了什么?

最佳答案

您可以直接分派(dispatch)到异步任务中的主线程,而不是使用信号量,

func uploadImage(image: UIImage) {
    let request = self.createRequest(image)
    let session : NSURLSession = NSURLSession.sharedSession()
    let task : NSURLSessionTask = session.dataTaskWithRequest(request, completionHandler: {
        (data, response, error) in

        if error != nil {
            print(error!.description)
        } else {
            let httpResponse: NSHTTPURLResponse = response as! NSHTTPURLResponse

            if httpResponse.statusCode != 200 {
                print(httpResponse.description)
            } else {
                print("Success! Status code == 200.")
            }
        }

        // dispatch to main thread to stop activity indicator
        dispatch_async(disptach_get_main_queue()) {
            self.activityIndicatorView.stopAnimating()
        }
    })! // end dataTaskWithResult

    task.resume()
} // end uploadImage

关于ios - UIActivityIndi​​catorView 在异步任务期间不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32013372/

相关文章:

ios - 如何自定义 UITextField?

string - Swift REPL 意外行为

ios - 如何在不选择表格 View 的单元格的情况下将搜索栏作为第一响应者辞职?

javascript - 尝试从 javascript 中的异步调用创建数组时的回调

c# - 调用套接字的ReceiveAsync()调用后,接收到的数据缓冲区始终为空吗?

android - 避免在 Android 应用程序的 Android Volley 中出现 Race condition

ios - 使用完成处理程序按顺序执行操作

ios - 动态添加图像到 uitextview 的各个位置

ios - 如何锁定 CocoaPod 库版本?

ios - 表格 View 单元格无法进入 numberOfRowsInSection