8.2 Grand Central Dispatch · mixi-inc/iOSTraining Wiki · GitHubのようにGCDを使うと少しだけNSThread
を直に叩くよりコンパクトになった。が、スレッド名の文字列がNSString
でないことからも分かるように、低レベルのAPIのようだ。
ios - NSOperation vs Grand Central Dispatch - Stack OverflowにあるようにNSOperationQueue
はGCDを使って実装されたものが提供されていて、こちらはObjective-Cのクラスになっている。使う手間はGCDと大差なかった。
[NSThread]
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(threadLoop:) object:nil]; [thread start]; - (void)threadLoop:(id)userInfo { [NSThread sleepForTimeInterval:5]; }
[GCD]
dispatch_queue_t queue = dispatch_queue_create("subthread", DISPATCH_QUEUE_SERIAL); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:5]; });
[NSOperationQueue]
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue setMaxConcurrentOperationCount:1]; [queue addOperationWithBlock: ^{ [NSThread sleepForTimeInterval:5]; }];