iPhone SDK - Running a repeating process in a background thread

孤人 提交于 2019-12-07 10:31:43

问题


I have an iPhone application which in which I want to perform a method in the background every 1 second.

So in my main thread, I have the following code upon my UIViewController viewDidLoad():

[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(repeatedMethod) userInfo:nil repeats:YES];

with the following methods:

-(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]
}

The way I have this set up, a new thread is spawned every second (unless the process is still running, thanks to my processIsRunning flag).

Now, my questions are:

(1) Is this the best way to do this? Or is there a more appropriate way to retain and reuse the background thread?

(2) What might be the fastest way to do this? Am I losing time by spinning up new background threads each time the method is called?

The code works fine as is, it's just a quite a bit slower when I ran everything on the main thread (which I ultimately don't want to do).

Any advice would be great! Has anyone dealt with this type of question before?

Many thanks, Brett


回答1:


Based on the question in your comment, I figured I'd expand my comment into a real answer. You can use GCD for this, and it's likely best way to go.

Something like this:

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];
}

One of the coolest things about blocks is that they capture local scope. That's not actually used in my example above, but it makes "passing" objects into GCD trivial. See my answer to this question: Is there an advantage to using blocks over functions in Objective-C? .



来源:https://stackoverflow.com/questions/9949248/iphone-sdk-running-a-repeating-process-in-a-background-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!