2

In my iPhone app, I have to perform function constantly in background.

For that I think I will have to use NSThread to call the function and keep it executing in background.

I dont want to stall my app and hence I want to use NSThread to keep my Main Thread free for user interaction.

How should I implement NSThread to perform the function in background?

EDIT:

The function is for fetching the data from a web server every 20 seconds and updating the tables in my iPhone app based on the data that is fetched from the web server.

halfer
  • 19,824
  • 17
  • 99
  • 186
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216

3 Answers3

4

I'd look at an NSOperationQueue first.

I'm guessing that your background task is really a small task repeated again and again. Make this into an NSOperation subclass and just add them onto an NSOperationQueue. That way you can control the background tasks more easily.

You also get the advantage with an NSOperationQueue that when there are no operations to run, the processor isn't just stuck in a while(YES) loop, waiting. This will help your app's UI be more responsive and will help battery life :)

However, if your background task is a single long running task that just needs to be started and then ignored, performSelectorInBackground isn't too bad an idea.

deanWombourne
  • 38,189
  • 13
  • 98
  • 110
0

Sounds like a bad idea, but it's very simple.

[self performSelectorInBackground:@selector(theMethod:) withObject:nil];

Just have a while(YES) in theMethod: and it will never stop executing.

EDIT:

Luckily for you it's just as simple to do something once every 20 seconds.

[NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(theMethod:) userInfo:nil repeats:YES];

This will execute theMethod: once every 20 seconds. I might also add that this is a much better idea.

Erik B
  • 40,889
  • 25
  • 119
  • 135
  • Except it will crush the processor and kill battery life :( – deanWombourne Feb 04 '11 at 13:33
  • That's why I think it sounds like a bad idea. I think it would be helpful to know what he's intending that method to do, so that one could propose a better solution. – Erik B Feb 04 '11 at 13:53
  • fair enough - we could do with a bit more information in the question – deanWombourne Feb 04 '11 at 15:16
  • @deanWombourne, @Erik B :Thanks for the input. I have edited my question to explain what my method needs to do. Look at the `EDIT` portion in my question Hope this makes it more clear. – Parth Bhatt Feb 07 '11 at 04:35
  • Hmmm, if it's every 20 seconds on a schedule then perhaps a background task run from an NSTimer would do it. Though if it's a web server then you should not run it as a background task but as an asynchronous connection triggered from the main thread? – deanWombourne Feb 07 '11 at 09:54
  • @deanWombourne : How can I create and implement asynchronous connection triggered from the main thread? – Parth Bhatt Feb 07 '11 at 10:34
  • Maybe that should be the title of the question? ;) – Erik B Feb 07 '11 at 10:54
  • Erik B is right - you should probably ask a new question on stack overflow. Or, even better, search it - this question's accepted answer has a link that will help - http://stackoverflow.com/questions/1707253/what-exactly-nsurlconnection-asynchronous-means – deanWombourne Feb 07 '11 at 10:59
0

you'll want to interact with the thread's run loop. if it's an NSThread, it is created automatically. thus, you are given access to the CF/NS-RunLoop - typically by calling + [NSThread currentRunLoop] from the secondary thread.

in that case, you add a CF/NS-Timer to the run loop, and let it run and repeat until your work is finished. when the timer fires, your thread is awoken, and you then do your work.

justin
  • 104,054
  • 14
  • 179
  • 226