swift和oc gcd使用
2018-03-07 11:23
288 查看
最近在做oc转swift项目,碰到swift中gcd的用法,整理了下gcd常用方法,希望对大家有帮助
///并行队列 + 异步执行,可同时开启多线程,任务交替执行 func asyncConcurrent() { /* swift */ print("asyncConcurrent---begin"); let queue = DispatchQueue(label: "com.lin.syc") queue.async { for i in 0..<2 { print("-------",i) } } queue.async { for i in 3..<5 { print("-------",i) } } /* oc */ // dispatch_queue_t queue= dispatch_queue_create("test.queue", DISPATCH_QUEUE_CONCURRENT); // // dispatch_async(queue, ^{ // for (int i = 0; i < 2; ++i) { // NSLog(@"1------%@",[NSThread currentThread]); // } // }); // dispatch_async(queue, ^{ // for (int i = 0; i < 2; ++i) { // NSLog(@"2------%@",[NSThread currentThread]); // } // }); // dispatch_async(queue, ^{ // for (int i = 0; i < 2; ++i) { // NSLog(@"3------%@",[NSThread currentThread]); // } // }); print("asyncConcurrent---end"); } ///串行队列 + 同步执行,不会开启新线程,在当前线程执行任务。任务是串行的,执行完一个任务,再执行下一个任务 func syncSerial() { print("syncSerial---begin"); /* swift */ let queue = DispatchQueue(label: "com.lin.syc") queue.sync { for i in 0..<2 { print("-------",i) } } queue.sync { for i in 3..<5 { print("-------",i) } } /* oc */ // dispatch_queue_t queue = dispatch_queue_create("test.queue", DISPATCH_QUEUE_SERIAL); // // dispatch_sync(queue, ^{ // for (int i = 0; i < 2; ++i) { // NSLog(@"1------%@",[NSThread currentThread]); // } // }); // dispatch_sync(queue, ^{ // for (int i = 0; i < 2; ++i) { // NSLog(@"2------%@",[NSThread currentThread]); // } // }); // dispatch_sync(queue, ^{ // for (int i = 0; i < 2; ++i) { // NSLog(@"3------%@",[NSThread currentThread]); // } // }); print("syncSerial---end"); } ///执行完栅栏前面的操作之后,才执行栅栏操作,最后再执行栅栏后边的操作 func barrier() { /* swift */ let queue = DispatchQueue(label: "com.lin.syc") queue.async { print("----1---",Thread.current) } queue.async { print("----2---",Thread.current) } queue.async(group: nil, qos: .default, flags: .barrier) { print("----3---",Thread.current) } queue.async { print("----4---",Thread.current) } /* oc */ // dispatch_queue_t queue = dispatch_queue_create("12312312", DISPATCH_QUEUE_CONCURRENT); // // dispatch_async(queue, ^{ // NSLog(@"----1-----%@", [NSThread currentThread]); // }); // dispatch_async(queue, ^{ // NSLog(@"----2-----%@", [NSThread currentThread]); // }); // // dispatch_barrier_async(queue, ^{ // NSLog(@"----barrier-----%@", [NSThread currentThread]); // }); // // dispatch_async(queue, ^{ // NSLog(@"----3-----%@", [NSThread currentThread]); // }); // dispatch_async(queue, ^{ // NSLog(@"----4-----%@", [NSThread currentThread]); // }); } ///分别异步执行2个耗时操作,然后当2个耗时操作都执行完毕后再回到主线程执行操作 func group() { /* swift */ let group = DispatchGroup() let queue = DispatchQueue(label: "com.lin.syc") queue.async(group: group, qos: .default, flags: []) { print("----1---",Thread.current) } queue.async(group: group, qos: .default, flags: []) { print("----2---",Thread.current) } group.notify(queue: .main) { } /* oc */ // dispatch_group_t group = dispatch_group_create(); // // dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // // 执行1个耗时的异步操作 // }); // // dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // // 执行1个耗时的异步操作 // }); // // dispatch_group_notify(group, dispatch_get_main_queue(), ^{ // // 等前面的异步操作都执行完毕后,回到主线程... // }); }
相关文章推荐
- Swift之GCD使用指南1
- Swift之GCD 使用指南2
- Swift-->GCD,NSThread,NSBlockOperation多线程使用(主线程回调)
- swift项目中使用OC/C的方法
- 在项目里交叉使用Swift和OC
- Swift和OC混合使用
- Swift之GCD 使用指南2
- Swift中如何使用OC中的类
- swift与oc的互用--使用oc特性编写swift
- 在Swift项目中使用OC,在OC项目中使用Swift
- Swift中GCD的使用
- GCD学习 - swift GCD使用指南
- iOS开发:Swift多线程GCD的使用
- 关于OC和Swift混编的基本使用
- swift闭包的使用 -- 类似于OC中的Block
- 在项目里交叉使用Swift和OC
- 在Swift项目中使用OC,在OC项目中使用Swift
- swift中使用OC的方法;在OC中调用swift方法
- 使用OC和Swift两种语言写一个发射烟花的小项目
- iOS 静态库的打包及使用(swift_oc)