views:

477

answers:

3

Hello, I need a background process to run every minute or so. I do not want this process to interfere with the responsiveness of the rest of the app. Would this be a good candidate for a thread? If so, how would you suggest I set up the thread - in particular, what classes/methods would be good to look into?

+4  A: 

It sounds like a pretty good candidate for NSOperation and NSOperationQueue - package your work unit up as an operation and fire it off whenever necessary. For doing the "every minute or so" bit, NSTimer is the unsurprising option.

Graham Lee
+2  A: 

It depends on how closely tied the background process is. It might be a better idea to make a separate program that runs in the background.

To get started, take a look at Apple's guide to threads.

Multithreaded programming isn't for the faint of heart, so you'll want to make sure you understand the ideas and caveats behind it — and probably try other solutions first. If this "background process" isn't very heavy, you could almost certainly get away with just using a timer on your main thread. If it's not closely tied to your app's internals, a "worker" program might be a good idea.

Chuck
A: 

It really depends on what the background process is doing, what resources does it need access to and so on. If you just want to call someMethod: every minute then use an NSTimer. If you really do need something like NSOperationQueue, I'd suggest looking into this replacement which addresses some bugs in the current implementation.

http://www.mikeash.com/?page=pyblog/raoperationqueue-an-open-source-replacement-for-nsoperationqueue.html

zorn
Apple has fixed the bug now, we're free to use NSOperationQueue once more - http://www.mikeash.com/pyblog/use-nsoperationqueue.html
Tobias Cohen