如何在单独的线程上运行代码?是以下哪种方法最好:
[NSThread detachNewThreadSelector: @selector(doStuff) toTarget:self withObject:NULL];
或者:
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(doStuff:)
object:nil;
[queue addOperation:operation];
[operation release];
[queue release];
我一直在使用第二种方法,但我正在阅读的Wesley Cookbook却使用第一种方法。
QOS_CLASS_BACKGROUND
而导致程序崩溃。而在该行代码上方有明确说明,可以使用其他值,例如DISPATCH_QUEUE_PRIORITY_DEFAULT
。由于匆忙之下没有注意到这一点,才会出现这个问题。 - Martin Berger