执行繁重的for循环时停止app冻结

时间:2015-02-21 14:20:34

标签: ios objective-c swift

我的应用程序中有一个for循环,每30秒运行一次。这个for循环运行一个非常繁重的进程(与MapKit相关,设置大量对象的坐标/更新属性)。问题是,整个应用程序在执行此过程时会冻结1-2秒。

Swift是否提供任何功能来阻止这种冻结?我已经对此事进行过一些阅读,并且已经阅读了#34;线程"可能就是答案。

关于如何在执行代码时阻止我的应用程序冻结的任何想法?

1 个答案:

答案 0 :(得分:3)

let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {

// set your long task here

    dispatch_async(dispatch_get_main_queue()) {
        // update some UI once finished the background task
    }
}