在单独的线程上运行代码的最佳方式是什么?是不是:
[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使用了第一种方法。
发布于 2010-10-06 11:40:45
在我看来,最好的方法是使用GCD调度,也就是中央调度(GCD)。它限制你使用iOS 4或更高版本,但它非常简单易用。在后台线程上执行一些处理,然后在主运行循环中对结果执行某些操作的代码非常简单和紧凑:
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Add code here to do background processing
//
//
dispatch_async( dispatch_get_main_queue(), ^{
// Add code here to update the UI/send notifications based on the
// results of the background processing
});
});如果您还没有这样做,请在libdispatch/GCD/block上查看WWDC 2010的视频。
发布于 2015-03-25 16:36:23
在iOS中多线程的最好方法是使用GCD (中央调度中心)。
//creates a queue.
dispatch_queue_t myQueue = dispatch_queue_create("unique_queue_name", NULL);
dispatch_async(myQueue, ^{
//stuffs to do in background thread
dispatch_async(dispatch_get_main_queue(), ^{
//stuffs to do in foreground thread, mostly UI updates
});
});发布于 2015-04-16 11:32:30
我会尝试人们发布的所有技术,看看哪种最快,但我认为这是最好的方法。
[self performSelectorInBackground:@selector(BackgroundMethod) withObject:nil];https://stackoverflow.com/questions/3869217
复制相似问题