以前谈到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/,大有裨益。