GCD vs performSelectorInBackground/performSelectorOnMainThread

僤鯓⒐⒋嵵緔 提交于 2019-11-28 05:01:33

When to use performSelectorInBackground:

Never. Do not use this method. It spawns an unbounded number of threads. Even before GCD was available, this was a horrible method.

When to use performSelectorOnMainThread:

Meh… Never, but just because it's inconvenient. There's nothing deeply wrong with this method. It's just not as useful as dispatch_async().

The difference between GCD and the old performSelector… methods (and NSThread in general) is that GCD manages a thread pool for you. In general, you should avoid manual threading in Cocoa. Instead, use NSOperationQueue or GCD (dispatch methods). They provide a more useful queue abstraction rather than forcing you to manually manage threads.

Be sure to read over Apple's Migrating Away from Threads for more info.

Actually after iOS 4.0 I can't find any single reason to use performSelectorInBackground/onMainThread. If you need to do something in background, use GCD( or, better, NSOperationQueue which is built on top of GCD since 4.0 and gives greater flexibility with little overhead), but be sure not to create retain cycles when using blocks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!