Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone SDK - Running a repeating process in a background thread

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

like image 259
Brett Avatar asked Dec 12 '22 03:12

Brett


1 Answers

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? .

like image 177
Andrew Madsen Avatar answered Feb 23 '23 16:02

Andrew Madsen