iPhone SDK - 在后台线程中运行重复进程

Bre*_*ett 4 iphone multithreading cocoa-touch objective-c nsthread

我有一个iPhone应用程序,我想在后台每秒执行一个方法1.

所以在我的主线程中,我有以下代码UIViewController viewDidLoad():

[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(repeatedMethod) userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)

使用以下方法:

-(void)repeatedMethod {
  if(!processIsRunning) {
    processIsRunning = YES;
    [self performSelectorInBackground:@selector(runProcessInBackground:) withObject:myImage];
  }
}

-(void)runProcessInBackground:(UIImage *)image {
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

  ... Some operations ...
  ...   also call a method on the main thread which sets isProcessRunning to NO

  [pool release]
}
Run Code Online (Sandbox Code Playgroud)

我有这个设置的方式,每秒产生一个新的线程(除非进程仍在运行,感谢我的processIsRunning标志).

现在,我的问题是:

(1)这是最好的方法吗?或者是否有更合适的方法来保留和重用后台线程?

(2)最快的方法是什么?每次调用方法时,我是否会通过调整新的后台线程来浪费时间?

代码工作得很好,当我在主线程上运行所有东西时,它只是相当慢一点(我最终不想这样做).

任何建议都会很棒!有没有人以前处理过这类问题?

非常感谢,布雷特

And*_*sen 13

根据您评论中的问题,我想我会将我的评论扩展为真正的答案.您可以使用GCD,这可能是最佳方式.

像这样的东西:

dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, backgroundQueue);
dispatch_source_set_timer(timerSource, dispatch_time(DISPATCH_TIME_NOW, 0), 1.0*NSEC_PER_SEC, 0*NSEC_PER_SEC);
dispatch_source_set_event_handler(timerSource, ^{
    [self repeatedMethod];
});
dispatch_resume(timerSource);

- (void)repeatedMethod {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [self doStuffWithAnImage:self.myImage];   

    [pool drain];
}
Run Code Online (Sandbox Code Playgroud)

关于块的最酷的事情之一是它们捕获局部范围.这实际上并没有在我上面的例子中使用,但它使"传递"对象变成GCD琐碎.请参阅我对这个问题的回答:在Objective-C中使用块而不是函数是否有优势?.