在闭包中更新 UI

Update UI in closure

我正在尝试更新我的 UI,同时执行一个带有状态关闭和完成关闭的函数。 UI 仅在关闭完成时更新。我知道发生这种情况是因为操作没有在主线程上发生并且 UI 需要在主线程上更新。我曾尝试将 UI 更新移动到主线程,但没有任何运气。我已经包含了我的代码的简化版本。

我该如何解决这个问题?如果我为要执行的代码指定一个自定义线程,它会解决问题吗?如果是这样,那是怎么做到的?

非常感谢您抽空阅读。

代码包含在下面。如果您需要有关此问题的更多信息,请告诉我。

func parse(array: [String], status:(status: String!, progress: Float!) -> (), completion:(result: [String]!) ->()) -> () {

        status(status: "Process is starting.", progress: 0)

        var newArray = [String]()

        for (index, txt) in enumerate(array) {

            //Update status
            let progress = Float(index + 1) / Float(array.count)

            status(status: "Checking string: \(txt)", progress: progress)

            //Do something with txt
            let newTxt = txt + "OK"

            newArray.append(newTxt)

        }

        status(status: "Complete!", progress: 1.0)

        //Send completion
        completion(result: newArray)

    }

    var startArray = [String]()

    for index in 0...10000 {

        startArray.append("\(index)")

    }

    parse(startArray, { (status: String!, progress: Float!) -> () in

        dispatch_async(dispatch_get_main_queue(), { () -> Void in

            println(status)
            self.statusLabel.text = status

            println(progress)
            self.progressView.progress = progress

        })

        }, { (result: [String]!) -> () in

            println("Process complete. Here is the result:\n\(result)")
    })

我的问题是在主线程上调用了 parse,因此在更新 UI 时,该线程已经被阻塞。我通过将解析放在另一个线程中解决了这个问题(在大卫的评论的帮助下),然后用 GCD 更新主线程上的 UI。