本文翻译自:How to use background thread in swift?
How to use threading in swift? 如何在swift中使用线程?
dispatchOnMainThread:^{
NSLog(@"Block Executed On %s", dispatch_queue_get_label(dispatch_get_current_queue()));
}];
参考:https://stackoom.com/question/1cw7J/如何在swift中使用后台线程
From Jameson Quave's tutorial 来自Jameson Quave的教程
Swift 2 斯威夫特2
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
//All stuff here
})
A lot has been modernized in Swift 3.0. 很多已经在Swift 3.0中进行了现代化改造 。 Running something on the background thread looks like this: 在后台线程上运行一些东西看起来像这样:
DispatchQueue.global(qos: .background).async {
print("This is run on the background queue")
DispatchQueue.main.async {
print("This is run on the main queue, after the previous code in outer block")
}
}
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
print("This is run on the 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")
})
})
As of Swift 1.1 Apple didn't support the above syntax without some modifications. 从Swift 1.1开始,如果没有一些修改,Apple不支持上述语法。 Passing QOS_CLASS_BACKGROUND
didn't actually work, instead use Int(QOS_CLASS_BACKGROUND.value)
. 传递QOS_CLASS_BACKGROUND
实际上并不起作用,而是使用Int(QOS_CLASS_BACKGROUND.value)
。
For more information see Apples documentation 有关更多信息,请参阅Apples文档
The best practice is to define a reusable function that can be accessed multiple times. 最佳实践是定义可以多次访问的可重用函数。
eg somewhere like AppDelegate.swift as a Global Function. 例如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?()
}
}
}
Note: in Swift 2.0, replace QOS_CLASS_USER_INITIATED.value above with QOS_CLASS_USER_INITIATED.rawValue instead 注意:在Swift 2.0中,用QOS_CLASS_USER_INITIATED.rawValue替换上面的QOS_CLASS_USER_INITIATED.value
A. To run a process in the background with a delay of 3 seconds: A.要在后台运行一个延迟3秒的进程:
backgroundThread(3.0, background: {
// Your background function here
})
B. To run a process in the background then run a completion in the foreground: 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. To delay by 3 seconds - note use of completion parameter without background parameter: C.延迟3秒 - 注意使用没有背景参数的完成参数:
backgroundThread(3.0, completion: {
// Your delayed function here to be run in the foreground
})
You have to separate out the changes that you want to run in the background from the updates you want to run on the UI: 您必须从要在UI上运行的更新中分离出要在后台运行的更改:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
// do your task
dispatch_async(dispatch_get_main_queue()) {
// update some UI
}
}
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), {
// Conversion into base64 string
self.uploadImageString = uploadPhotoDataJPEG.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.EncodingEndLineWithCarriageReturn)
})