我有一个定期运行的任务,它最初设计为在一个单独的运行循环上运行,而不是使用 NSThread 和 NSTimer 的主运行循环。
调整它以利用 GCD 的最佳方法是什么?
当前代码:
-(void)initiateSomeTask
{
[NSThread detachNewThreadSelector:@selector(startTimerTask)
toTarget:self withObject:nil];
}
-(void)startTimerTask
{
// We won't get back the main runloop since we're on a new thread
NSRunLoop *myRunLoop = [NSRunLoop currentRunLoop];
NSPort *myPort = [NSMachPort port];
[myRunLoop addPort:myPort forMode:NSDefaultRunLoopMode];
NSTimer *myTimer = [NSTimer timerWithTimeInterval:10 /* seconds */
target:self selector:@selector(doMyTaskMethod)
userInfo:nil repeats:YES];
[myRunLoop addTimer:myTimer forMode:NSRunLoopCommonModes];
[myRunLoop run];
}
detachNewThreadSelector
除了替换之外我还能做些什么dispatch_async
吗?