iOS GCD常用方法总结(oc+swift)

功能OCSwift
获取主队列dispatch_queue_t q = dispatch_get_main_queue();DispatchQueue.main
获取全局队列dispatch_queue_t q = dispatch_get_gloable_queue(优先级, 0);
1. DISPATCH_QUEUE_PRIORITY_HIGH 2 高
2. DISPATCH_QUEUE_PRIORITY_DEFAULT 0 默认
3. DISPATCH_QUEUE_PRIORITY_LOW -2 低
4. DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN 2 后台优先级
DispatchQueue.global(qos: 优先级)
1. .userInteractive 需要用户交互的
2. .userInitiated 即将需要
3. .default 默认优先级
4. .utility 需要执行一段时间后,再通知用户
创建队列dispatch_create(“标识”, DISPATCH_QUEUE_SERIAL/CONCURRENT);DispatchQueue(label: “标识”, qos: 优先级, attributes: .concurrent 默认是串型)
异步/同步dispatch_async(q, ^{…});
dispatch_sync(q, ^{…});
q.async{…}
q.sync{…}
Barrierdispatch_barrier_async(q, ^{…});queue.async(flags: .barrier) {…}
Afterdispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 3ull*NSEC_PER_SEC);
dispatch_after(time, q, ^{…});
q.asyncAfter(deadline: .now() + 1.0) {…}
创建组dispatch_group_t group = dispatch_group_create();let group = DispatchGroup()
组进入/释放dispatch_group_enter(g);
dispatch_group_leave(g);
group.enter()
group.leave()
组等待long result = dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
DISPATCH_TIME_NOW则无需等待判断group是否结束,建议用notify
group.wait(timeout: 可省)
组等待通知dispatch_group_notify(group, queue, ^{…});group.notify(queue: q) {…}
信号量创建dispatch_semaphore_t sem = dispatch_semaphore_create(1/0);let sem = DispatchSemaphore(value: 1)
PV操作dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
dispatch_semaphore_signal(sem);
sem.wait()
sem.signal()
Oncedispatch_once_t once;
dispatch_once(&once, ^{…});
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页