标签:gcd dispatch_async dispatch_sync dispatch_async_f dispatch_sync_f
为什么要写这个系列,因为百度了一下,找了很多都是些片面的Blog,拷贝来拷贝去的,写的也很粗糙。所以,我要写这个系列,尽量把官网文档中GCD的强大功能完整的表达出来。方便自己,也方便别人,如果发现有问题,欢迎提出
的一篇文章会进行总结,总结出GCD的经典使用场景。源代码只提供Swift版本。因为要上班,计划一个月内完成。每周两篇。
原创Blog,转载请注明出处
这个专栏地址
http://blog.csdn.net/column/details/swift-gcd.html
#define DISPATCH_QUEUE_PRIORITY_HIGH 2 #define DISPATCH_QUEUE_PRIORITY_DEFAULT 0 #define DISPATCH_QUEUE_PRIORITY_LOW (-2) #define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN
typealias dispatch_queue_t = NSObject //轻量级的用来描述执行任务的队列 typealias dispatch_block_t = () -> Void //队列执行的闭包(Objective C中的block)
func dispatch_async(_ queue: dispatch_queue_t!, _ block: dispatch_block_t!)参数:
func dispatch_async_f(_ queue: dispatch_queue_t!, _ context: UnsafeMutablePointer<Void>, _ work: dispatch_function_t)参数
work 执行的函数(C语言函数)
dispatch_sync 和 dispatch_sync的参数和上述对应一致,所以不再列出
总得来说带有后缀_f(比如dispatch_sync_f,dispatch_after_f)就是提交给队列一个C语言函数,因为极少用到这种形式,这里仅给出一个简单例子,后面的涉及到_f的都略过。func dispatch_get_global_queue(_ identifier: Int, _ flags: UInt) -> dispatch_queue_t!这个函数的第一个参数是队列的优先级,第二个参数尚没有意义,直接写0就可以了。
class ViewController: UIViewController{ var imageview = UIImageView(frame: CGRectMake(40,40,200,200)) override func viewDidLoad(){ super.viewDidLoad() imageview.contentMode = UIViewContentMode.ScaleAspectFit self.view.addSubview(imageview) let url = "http://f.hiphotos.baidu.com/image/pic/item/e1fe9925bc315c60191d32308fb1cb1348547760.jpg" let imageURL = NSURL(string:url) var globalQueueDefault = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0) dispatch_async(globalQueueDefault){ var imageData = NSData(contentsOfURL:imageURL!) var image = UIImage(data:imageData!) if let successfulImage = image{ sleep(2) dispatch_async(dispatch_get_main_queue()){ self.imageview.image = successfulImage } } } } override func didReceiveMemoryWarning(){ super.didReceiveMemoryWarning() } }执行,观察下效果:view立刻载入,然后过一段时间,图片下载完了,UI改变
dispatch_sync(globalQueueDefault,0){
#import "hwcTest.h"
#include <stdio.h> #include <unistd.h> typedef void (*hwcTestForGCD)(void*); hwcTestForGCD getFuncPointer();
#include "hwcTest.h" void realFunction(void *input){ for(int i = 0;i < 5;i++){ printf("%d\n",i); sleep(1); } } hwcTestForGCD getFuncPointer(){ return realFunction; }ViewController.swift
class ViewController: UIViewController{ var imageview = UIImageView(frame: CGRectMake(40,40,200,200)) override func viewDidLoad(){ super.viewDidLoad() var globalQueueDefault = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0) dispatch_async_f(globalQueueDefault,nil,getFuncPointer()) println("dispatch is over") } override func didReceiveMemoryWarning(){ super.didReceiveMemoryWarning() } }然后执行,会发现输出
0 dispatch is over 1 2 3 4
0 1 2 3 4 5 dispatch is over这里更能体会到了,什么是同步,什么是异步了吧。
func dispatch_queue_create(_ label: UnsafePointer<Int8>, _ attr: dispatch_queue_attr_t!) -> dispatch_queue_t!参数
class ViewController: UIViewController{ var imageview = UIImageView(frame: CGRectMake(40,40,200,200)) override func viewDidLoad(){ super.viewDidLoad() dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)){ for var i = 0;i < 5;i++ { NSLog("First task:%d",i) sleep(1) } } dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)){ for var j = 0;j < 5;j++ { NSLog("Second task:%d",j) sleep(1) } } println("dispatch is over") } override func didReceiveMemoryWarning(){ super.didReceiveMemoryWarning() } }
这里执行输出为:
First task:0 Second task:0 First task:1 Second task:1 First task:2 Second task:2 First task:3 Second task:3 First task:4 Second task:4这段代码执行时间4.03s
class ViewController: UIViewController{ var imageview = UIImageView(frame: CGRectMake(40,40,200,200)) override func viewDidLoad(){ super.viewDidLoad() var serialQueue = dispatch_queue_create(label: "com.test.helloHwc",attr:DISPATCH_QUEUE_SERIAL) dispatch_async(serialQueue){ for var i = 0;i < 5;i++ { NSLog("First task:%d",i) sleep(1) } } dispatch_async(serialQueue){ for var j = 0;j < 5;j++ { NSLog("Second task:%d",j) sleep(1) } } println("dispatch is over") } override func didReceiveMemoryWarning(){ super.didReceiveMemoryWarning() } }
这里输出为
First task:0 First task:1 First task:2 First task:3 First task:4 Second task:0 Second task:1 Second task:2 Second task:3 Second task:4这段代码执行时间8.06秒
看出来并行和串行执行的差别了吧。
所以,记住一点,把过程不相关的任务,提交到并行的队列中会显著提高效率
下一篇预计更新时间,本周末
BTY:如果图片下载失败,可能因为时间的原因,那张图片从服务器上删除了,自己找一个URL即可
原图
完整详解GCD系列(一)dispatch_async;dispatch_sync;dispatch_async_f;dispatch_sync_f
标签:gcd dispatch_async dispatch_sync dispatch_async_f dispatch_sync_f
原文地址:http://blog.csdn.net/hello_hwc/article/details/41073287