4

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

Brett
  • 11,637
  • 34
  • 127
  • 213
  • Take a look at my answer to this question: http://stackoverflow.com/questions/8759264/worker-thread-ios/8759329#8759329 – Andrew Madsen Mar 30 '12 at 19:27
  • @Andrew, Thanks! One question though: how could I pass in an object vis GCD? In my code above, this object would be 'myImage'. – Brett Mar 30 '12 at 19:33

1 Answers1

13

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

Community
  • 1
  • 1
Andrew Madsen
  • 21,309
  • 5
  • 56
  • 97
  • This is actually quite informative! Thank you. But it leads to one (hopefully) last question. In your referenced answer to the other question, you show that the block will retain the local scope at the time of creation (i.e., num1=42 in your other answer). In my case, I would like self.image to be updated from time to time on the main thread. Would [self doStuffWithAnImage:self.myImage] be able to access the most recent image (as set by the main thread), or does it retain the self.image that existed at the time of the GCD thread creation? Thanks again! – Brett Mar 30 '12 at 19:56
  • `self.myImage` is equivalent to `[self myImage]`. Since it is actually calling the `-myImage` accessor method, it will get and use the current value at the time the block is executed. In any case, that call is made in repeatedMethod, which is called by the block. Blocks capture variables used directly in the body of the block, but they don't recursively descend through the methods called and capture scope there (that doesn't make sense because variables inside methods are on the stack and don't exist until the method is called anyway). So in short, it _will_ get the most recent myImage. – Andrew Madsen Mar 30 '12 at 20:01
  • I should add to that that the fact that you'll be setting myImage in one thread and reading (and possibly setting it?) in another means you need to be careful with thread safety. As long as you're only reading it in the background and not setting it, you should be OK simply by making the @property atomic. If you need to set it from the background, the easiest way is probably to dispatch the set call back to the main thread. – Andrew Madsen Mar 30 '12 at 20:03
  • This is awesome stuff, thank you. And I just learned a lot. I do need to set my isProcessRunningFlag to NO from the background thread. So to this I would need to dispatch the set callback? Like so:? dispatch_async(dispatch_get_main_queue(), ^{ isProcessRunning = NO; }); – Brett Mar 30 '12 at 20:15
  • Hmm...I just compiled and ran a _very_ minimal test program using the code from my answer (on my Mac, not iOS) and it's working fine, so I'm not sure what the problem might be. – Andrew Madsen Mar 30 '12 at 20:37
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9516/discussion-between-andrew-madsen-and-brett) – Andrew Madsen Mar 30 '12 at 20:38