目标 C - performSelectorInBackground V.S detachNewThreadSelector? [英] Objective C - performSelectorInBackground V.S detachNewThreadSelector?
问题描述
detachNewThreadSelector 和 performSelectorInBackground 都用于在后台调用一个方法.
Both detachNewThreadSelector and performSelectorInBackground are used to call a method in the background.
这两种方法有什么区别吗?还是它们的工作方式相同?
Is there any difference between the 2 methods? or do they both work the same way?
推荐答案
它们本质上是相同的,但范例略有不同.在幕后,他们做着完全一样的事情.唯一真正的区别是 -[performSelectorInBackground:withObject:]
遵循所有其他 performSelector 样式方法,因为它们是在 NSObject
和您实际上定义了您希望执行的选择器的消息.
They're both essentially the same but slightly different paradigms. Behind the scenes they do exactly the same thing. The only real difference is that -[performSelectorInBackground:withObject:]
follows all the other performSelector style methods in that they're defined on NSObject
and you actually message that defines the selector you wish to perform.
一般来说,您几乎不必调用这些方法中的任何一个.倾向于使用 Grand Central Dispatch 或 NSOperation
和 NSOperationQueue
来分解其他线程上的昂贵操作.GCD 和 NSOperation
类都为您提供内存管理、线程池管理以及使用旧式调度方法时您会错过的许多其他功能.
In general, you should almost never have to call either of these methods. Favor using Grand Central Dispatch or NSOperation
and NSOperationQueue
to factor out expensive operations on other threads. Both GCD and the NSOperation
classes give you memory management, thread pool management and many other things you'll miss using the older style dispatch methods.
这篇关于目标 C - performSelectorInBackground V.S detachNewThreadSelector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!