如何使用线程在迅速?

dispatchOnMainThread:^{

    NSLog(@"Block Executed On %s", dispatch_queue_get_label(dispatch_get_current_queue()));

}];

当前回答

你必须将你想要在后台运行的更改与你想要在UI上运行的更新分开:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
    // do your task

    dispatch_async(dispatch_get_main_queue()) {
        // update some UI
    }
}

其他回答

斯威夫特5

为了简化,创建一个“DispatchQueue+Extensions.swift”文件,内容如下:

import Foundation

typealias Dispatch = DispatchQueue

extension Dispatch {

    static func background(_ task: @escaping () -> ()) {
        Dispatch.global(qos: .background).async {
            task()
        }
    }

    static func main(_ task: @escaping () -> ()) {
        Dispatch.main.async {
            task()
        }
    }
}

用法:

Dispatch.background {
    // do stuff

    Dispatch.main { 
        // update UI
    }
}

来自Jameson Quave的教程

斯威夫特2

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
    //All stuff here
})

快4.倍

把这些放到文件中:

func background(work: @escaping () -> ()) {
    DispatchQueue.global(qos: .userInitiated).async {
        work()
    }
}

func main(work: @escaping () -> ()) {
    DispatchQueue.main.async {
        work()
    }
}

然后在需要的地方调用它:

background {
     //background job
     main {
       //update UI (or what you need to do in main thread)
     }
}

Swift 3.0 +

在Swift 3.0中,很多东西都被现代化了。在后台队列上运行一些东西看起来像这样:

DispatchQueue.global(qos: .userInitiated).async {
    print("This is run on a background queue")

    DispatchQueue.main.async {
        print("This is run on the main queue, after the previous code in outer block")
    }
}

Swift 1.2到2.3

let qualityOfServiceClass = QOS_CLASS_USER_INITIATED
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
    print("This is run on a background queue")

    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        print("This is run on the main queue, after the previous code in outer block")
    })
})

Pre Swift 1.2 -已知问题

从Swift 1.1开始,苹果在没有做一些修改的情况下不支持上述语法。传递qos_class_user_initialized实际上不起作用,而是使用Int(qos_class_user_initialized .value)。

有关更多信息,请参阅apple文档

最佳实践是定义一个可多次访问的可重用函数。

可重用的功能:

例如,AppDelegate.swift是一个全局函数。

func backgroundThread(_ delay: Double = 0.0, background: (() -> Void)? = nil, completion: (() -> Void)? = nil) {
    dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.value), 0)) {
        background?()

        let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
        dispatch_after(popTime, dispatch_get_main_queue()) {
            completion?()
        }
    }
}

注意:在Swift 2.0中替换QOS_CLASS_USER_INITIATED。使用QOS_CLASS_USER_INITIATED。rawValue相反

用法:

a .在后台运行一个进程,延迟3秒:

    backgroundThread(3.0, background: {
            // Your background function here
    })

B.在后台运行一个进程,然后在前台运行一个补全:

    backgroundThread(background: {
            // Your function here to run in the background
    },
    completion: {
            // A function to run in the foreground when the background thread is complete
    })

C.延迟3秒-注意使用completion参数而不使用background参数:

    backgroundThread(3.0, completion: {
            // Your delayed function here to be run in the foreground
    })