Allowing 3rd party iPhone applications to run in the background is an often requested feature in the official SDK. Apple has responded that they can’t do that primarily due the increased, and unpredictable, battery drain. However Apple is also notorious for claiming that they are not working on something and they have absolutely no interest in working on it, until they surprise everybody with a working solution. With that in mind I decided to poke around a bit in the new 3.0 SDK.
There are 1,000 new API:s in 3.0, so there’s a fair amount of ground to cover, but a new framework called AsyncPRocessIngLibrary caught my attention. I have not been able to find any documentation for this framework, but it’s not in the PrivateFrameworks directory, so it’s presumably fair game.
Here’s an example of what you can do with this library that was discovered today:
UInt32 numProcessors = MPProcessorsScheduled(); /* Schedule task across available processors. Major hint of new hardware... */ for (n = 0; n < numProcessors; n++ ) { MPCreateTask( MyTask, kMPStackSize, NULL, NULL, taskOptions, taskID ); }
This is all pretty standard multi processing code. What’s new and interesting here is the taskOptions:
enum { MPTaskOptionNone = 0, MPTaskOptionTerminateOnAppExit = 1 << 0, MPTaskOptionTerminateOnThreadDeath = 1 << 1, MPTaskOptionTerminateOnSemaphore = 1 << 2, MPTaskOptionKeepRunning = 1 << 3, MPTaskOptionLurad = 1 << 4, MPTaskOptionGetauscht = 1 << 5 }; typedef UInt32 MPTaskOption;
Most of these flags are self-explanatory, but unless you set the 4/1 bit, they don’t behave as expected.
Example:
UInt32 taskOptions = (MPTaskOptionKeepRunning | MPTaskOptionLurad);
It’s important that before attempting to launch your background process, you first need to ensure that the device you’re running on is capable of this:
if ([[UIDevice currentDevice] isProcessFoolEnabled]) { // start your process here }
April 1st, 2009 at 06:05
🙂 Well done. I especially like the “4/1” bit reference.
April 1st, 2009 at 06:35
boy i hate this day…
April 1st, 2009 at 12:00
see and this opens up alot of what can be done. the use of more api will finally bring in relevant data to users beyond rss and maps. thanks for the write up
phodder.com
May 5th, 2009 at 01:17
i think i’ll use it for my clock app because for the moment i think it is not possible to set up a fire alarm without the clock running
May 28th, 2009 at 06:22
where do i find this framework? i’m looking at the frameworks directory of the 3.0 sdk and i can’t find it
May 28th, 2009 at 11:41
You mean the AsyncPRocessIngLibrary ?
You might want to check the date this post was written…
July 2nd, 2009 at 12:56
Thanks for the Apr 1st joke, but you almost got me until I saw the “4/1”.
September 10th, 2010 at 06:16
Boo. Just wasted 2 minutes of my life.