Swift3 的gcd变化很大,这里列举一下,大家可以有个对照:
OC
dispatch_queue_t queue = dispatch_queue_create("com.leacode.gcd.serialqueue", DISPATCH_QUEUE_SERIAL); dispatch_async(queue, ^{
// 在queue线程执行
dispatch_async(dispatch_get_main_queue(), ^{
// 回到主线程
});
});
Swift3
let queue = DispatchQueue(label: "com.leacode.gcd.serialqueue")
queue.async {
// 在queue线程执行
DispatchQueue.main.async {
// 回到主线程
}
}
OC
dispatch_queue_t concurrentQueue = dispatch_queue_create("com.leacode.gcd.concurrentqueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentQueue, ^{
// 在queue线程执行
dispatch_async(dispatch_get_main_queue(), ^{
// 回到主线程
});
});
Swift3
let queue = DispatchQueue(label: "com.leacode.gcd.concurrentqueue", attributes: [.concurrent])
queue.async {
// 在queue线程执行
DispatchQueue.main.async {
// 回到主线程
}
}
从ios8开始苹果引入了一个新的概念 QoS(quality of service),有了更贴近使用场景的描述以及更细致的划分,代码如下 OC
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
dispatch_queue_attr_t globalDispatchQueueBackground = dispatch_queue_attr_make_with_qos_class(nil, QOS_CLASS_BACKGROUND, 0);
dispatch_queue_attr_t globalDispatchQueueUtility = dispatch_queue_attr_make_with_qos_class(nil, QOS_CLASS_UTILITY, 0);
dispatch_queue_attr_t globalDispatchQueueDefault = dispatch_queue_attr_make_with_qos_class(nil, QOS_CLASS_DEFAULT, 0);
dispatch_queue_attr_t globalDispatchQueueUserInitiated = dispatch_queue_attr_make_with_qos_class(nil, QOS_CLASS_USER_INITIATED, 0);
dispatch_queue_attr_t globalDispatchQueueUserInteractive = dispatch_queue_attr_make_with_qos_class(nil, QOS_CLASS_USER_INTERACTIVE, 0);
dispatch_queue_attr_t globalDispatchQueueUnspecified = dispatch_queue_attr_make_with_qos_class(nil, QOS_CLASS_UNSPECIFIED, 0);
} else {
dispatch_queue_t globalDispatchQueueHigh = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_queue_t globalDispatchQueueDefault = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t globalDispatchQueueLow = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
dispatch_queue_t globalDispatchQueueBackground = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
}
Swift3
if #available(iOS 8.0, *) {
let globalDispatchQueueBackground = DispatchQueue.global(qos: DispatchQoS.QoSClass.background)
let globalDispatchQueueUtility = DispatchQueue.global(qos: DispatchQoS.QoSClass.utility)
let globalDispatchQueueDefault = DispatchQueue.global(qos: DispatchQoS.QoSClass.default)
let globalDispatchQueueUserInitiated = DispatchQueue.global(qos: DispatchQoS.QoSClass.userInteractive)
let globalDispatchQueueUserInteractive = DispatchQueue.global(qos: DispatchQoS.QoSClass.userInteractive)
let globalDispatchQueueUnspecified = DispatchQueue.global(qos: DispatchQoS.QoSClass.unspecified)
} else {
let globalDispatchQueueBackground = DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.background)
let globalDispatchQueueLow = DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.low)
let globalDispatchQueueDefault = DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default)
let globalDispatchQueueHigh = DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.high)
}
这部分Swift3的变化较大 OC
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 3ull * NSEC_PER_SEC);
dispatch_after(time, dispatch_get_main_queue(), ^{
NSLog(@"%@", @"3秒后执行");
});
Swift3
let time = DispatchTime.now() + 3
DispatchQueue.main.asyncAfter(deadline: time) {
// 3秒后执行
print("3秒后执行")
}
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 6) {
// 6秒后执行2
print("6秒后执行")
}
OC
dispatch_suspend(queue);
dispatch_resume(queue);
Swift3
queue.suspend()
queue.resume()
OC
dispatch_queue_t queue1 = dispatch_queue_create("com.leacode.gcd.queue1", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t queue2 = dispatch_queue_create("com.leacode.gcd.queue2", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t queue3 = dispatch_queue_create("com.leacode.gcd.queue3", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t queue4 = dispatch_queue_create("com.leacode.gcd.queue4", DISPATCH_QUEUE_SERIAL);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue1, ^{
});
dispatch_group_async(group, queue2, ^{
});
dispatch_group_async(group, queue3, ^{
});
dispatch_group_async(group, queue4, ^{
});
dispatch_time_t group_time = dispatch_time(DISPATCH_TIME_NOW, 1ull*NSEC_PER_SEC);
long result = dispatch_group_wait(group, group_time);
if (result == 0) {
/*
* 属于Dispatch Group的全部处理执行结束
*/
} else {
/*
* 属于Dispatch Group的某一个处理还在进行中
*/
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
});
Swift3
let queue1 = DispatchQueue(label: "com.leacode.group.queue1")
let queue2 = DispatchQueue(label: "com.leacode.group.queue2")
let queue3 = DispatchQueue(label: "com.leacode.group.queue3")
let group = DispatchGroup()
queue1.async(group: group) {
}
queue2.async(group: group) {
}
queue3.async(group: group) {
}
/*
* 以下代码可以判断间隔时间为1秒后group是否执行完毕,并执行相应的处理
*/
let afterTime = DispatchTime.now() + 1
let result = group.wait(timeout: afterTime)
if result == .success {
/*
* 属于Dispatch Group的全部处理执行结束
*/
} else {
/*
* 属于Dispatch Group的某一个处理还在进行中
*/
}
group.notify(queue: DispatchQueue.main) {
}
这里我们用单例来举例,oc和swift中都是通过创建一个static对象来创建单例子,在swift3中只需要写一个static变量就可以了: OC
+ (id)sharedInstance
{
static MyClass *myclass = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
myclass = [[self alloc] init];
});
return myclass;
}
Swift
class MyClass {
static let singleton = MyClass()
private init() {
print("foo")
}
}
在swift中已经取消掉dispatch_once了,可以看苹果的说明链接
The free function dispatch_once is no longer available in Swift. In Swift, you can use lazily initialized globals or static properties and get the same thread-safety and called-once guarantees as dispatch_once provided
可以通过设置懒加载的全局属性或者静态属性来达到线程安全且执行一次的效果。