1

I have a a few threads that needs to run constantly or be locked at my disposal. When I lock the phone or swap applications the thread seems to halt until the application is back in focus.

I have a class Worker that is a sub class of NSThread. There is a method called start which is called by the firstViewController that creates the Worker object.

//method start
[self performSelectorInBackground:@selector(run) withObject:self];

What do I need to do to make my thread run all the time, rather than only running while in focus?

Thanks :)

Andrew
  • 1,764
  • 3
  • 24
  • 38
  • you cannot continue to run in the background when your app is suspended without using the background processing tasks API. – nielsbot Nov 16 '12 at 12:18
  • cf. http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html – nielsbot Nov 16 '12 at 12:19
  • I need to ... I need to track the users location for collision dection ect... its a pervasive location based game. Part of my dissertation. What are my alternatives? – Andrew Nov 16 '12 at 12:28

1 Answers1

1

When your app enters the background, all threads as suspended - unless you've configured your application to use multi-tasking, and your work is being done using the multi-tasking methods. This is detailed at Apple developer.

In short, you basically can't have a thread running constantly in the background on iOS if you want to be accepted in the App store, unless you're a navigation or VOIP application. You can have a thread continue to run for around 10 minutes after you enter the background, but that's it.

Adam Wright
  • 48,938
  • 12
  • 131
  • 152
  • Its a location based game. I can't see another work around for it. – Andrew Nov 16 '12 at 12:21
  • Then you can probably use the `location` type background task option. Though I don't know if that will pass muster for the App Store. I'd guess probably yes, but worth an e-mail first. A pity it's not easier, but this is the price for iOS development (and maintaining Apple's battery life claims :). – Adam Wright Nov 16 '12 at 12:27
  • Like the whole idea is that the game can be played in the back ground. Who would you suggest emailing, generally the responses are useless? Might it be worth getting a staff member to contact apple? When you say for 10 minutes.. is that sort of the apple store guidelines, or will IOS teriminate it? – Andrew Nov 16 '12 at 12:34
  • iOS will terminate it. As mentioned, the `location` backgrounding type will allow you to keep running, and receive location updates. This would seem to work for your case. You can send an email to appreview@apple.com, and try your luck with a response. Or trawl the App Store and see if other games similar to yours have been approved. There's nothing in the guidelines that would prohibit it, but these "new" edge cases can often provoke odd responses. – Adam Wright Nov 16 '12 at 12:44
  • One of my mates showed me and app today that was like a fitness app that tracks you. Which is sort of along the same lines I just need to maybe receive locations from the server to see if the player is in range of entities before notifying them. - Does that make sence? My supervisor says that is not the end of the world.. As long as I can still do some in house testing. (apple adhoc upto 100 devices) – Andrew Nov 16 '12 at 14:15
  • I imagine that is something along theses lines.... http://stackoverflow.com/questions/6347503/how-do-i-get-a-background-location-update-every-n-minutes-in-my-ios-application/6466152#6466152 but I need to be able to check for and receive data every so often. For example the mobile will contact the server every X minutes to see if there is anything new to transfer back to the phone. – Andrew Nov 16 '12 at 14:35
  • If it's ad-hoc only, then go for it with any combination of backgrounding modes that works. It's only for App Store distribution that these things bite. – Adam Wright Nov 16 '12 at 14:39
  • Well ideally I would like to have it on the app store by january – Andrew Nov 16 '12 at 15:37