GCD杂感

以前谈到GUI的多线程技术,http://blog.csdn.net/ani_di/article/details/8654214。前面三种是非常老的技术,代码很多,而且容易出错。唯独最后一种GCD(中央调度),代码精简,理解容易。

其实后面还可以加上NSObject的performSelector:

– performSelector:withObject:afterDelay:
– performSelector:withObject:afterDelay:inModes:
– performSelectorOnMainThread:withObject:waitUntilDone:
– performSelectorOnMainThread:withObject:waitUntilDone:modes:
– performSelector:onThread:withObject:waitUntilDone:
– performSelector:onThread:withObject:waitUntilDone:modes:
– performSelectorInBackground:withObject:
+ cancelPreviousPerformRequestsWithTarget:
+ cancelPreviousPerformRequestsWithTarget:selector:object:

它几乎等同于GCD的另一种形式

performSelector:onThread:withObject:waitUntilDone:

// waitUntilDone: NO    

dispatch_async(queue, ^{[myObject doSomething:foo withData:bar];}); 

// waitUntilDone: YES   

dispatch_sync(queue, ^{[myObject doSomething:foo withData:bar];});

performSelector:withObject:afterDelay:

dispatch_time_t delay;
delay = dispatch_time(DISPATCH_TIME_NOW, 50000 /* 50μs */);
dispatch_after(delay, queue, ^{[myObject doSomething:foo withData:bar];});

performSelectorInBackground:withObject:

dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{ [myObject doSomething:foo withData:bar];});

进一步发现,现在网络上关于GCD的讨论几乎都源自 WWDC 2010 Session 211 Simplifying iPhone App Development with Grand Central Dispatch。 不禁感叹,WWDC每年都开,每次都能带给开发者许多不一样的东西。有空把这些都看了https://developer.apple.com/videos/,大有裨益。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值