views:

1275

answers:

7

Ever since the first beta came out I've been trying to find out if "real" multitasking is possible. I.E., can you put a program in the background and have it hang on to a network connection indefinitely? I'm thinking about IM or IRC apps, for example. I've compiled an app myself on OS 4, and without changing a thing it appeared to stay running in the background, but for all I know it was just suspended to memory. The docs say the best you can do is request up to 10 minutes, but in the developer presentation they showed off Skype sitting in the background and then notifying the user that a call was coming in. Does anyone know for sure how this is all going to work?

+1  A: 

iOS 4 applications can either be running or suspended. The operating system will try to keep as many requested applications as possible in memory, while all other applications are suspended.

Applications that run in the background can access features such as navigation, audio, and VOIP (but NOT instant messaging). So it looks like you might be out of luck.

-- PC World Multitasking on Apples iPhone 4

Kevin Sylvestre
+7  A: 

It appears the answer is no. The API for Skype is a very special case, called the "voip" mode, and requires special behavior, such as marking the socket in use for VoIP.

You can receive alarm notifications in the background (such as time passed). The amount of time you are in the background running state is severely limited by the OS.

Android's background model is complete and in many ways much nicer.

Apple has a guide named "Supporting Multitasking In Your Applications" which you should be able to locate.

Yann Ramin
Fully-operational background mode is easier for developers to deal with, but brutal on battery life. That's the design tradeoff Apple has made here. Not to be an Apple apologist, but you can understand the design choice if you think like a user instead of a developer.
Dan Ray
I want it as a user. I want proper IRC and chat applications, and I want developers to be free to do what they need to do to create the applications that I want. Maybe I'm a geeky user, and not typical, but I'm still thinking like a user.
@pkulak and conversely you want batter life that is only good for a couple of hours for what you want. Real non-developer end users, want a phone that lasts all day and does what they need, they don't care HOW it does it. If it appears to do what you "want" but doesn't do it how you think it should be done, what is the difference.
fuzzy lollipop
why do you need IM messages in the background when you can't even see them. notifications that messages are available is plenty, then when you do switch back to your IM app it catches up with the new messages, you get what you need, and good battery life, what is the big deal here?
fuzzy lollipop
@fuzzy lollipop: My Android phones does just fine receiving messages in the background, and includes a notification system capable of having more than one notification queued at a time.
Yann Ramin
@theatrus I never said anything about Android and what it did and didn't do but since you bring it up,the battery life on the N1 ( and most other Android phones ) is atrocious. One of my developer friends is abandoning his N1 because it only lasts about half a day and his 3G lasts a day and a half to two days doing the same things.
fuzzy lollipop
you can create a long running background task... https://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
William Denniss
+1  A: 

You may be interested in this blog post that outlines how "multitasking" works in systems such as iPhone OS 4 and Android.

anthony
Note that Android does not have the same limits on services as iOS. It is quite possible to have a network connected service running continuously in the background, unlike iOS. The service can be started and stopped by the application (or its self), and the service can launch intents (much more flexible than the iOS counterpart). The notifications mentioned are named "broadcasts" in Android, and do not require a running service.
Yann Ramin
+4  A: 

Apple's iOS 4 developer docs outline this all very clearly.

When your app is closed or switched away from, it is almost immediately "suspended", meaning the OS freezes the app's state. When the user switches back to your app, your code keeps running just where it kept off. You don't need to add any code to your app to do this, just compile it against OS 4.

The above is true in most cases. There are two reasons the "suspended" model may not apply:

1) If the device starts to run low on memory, the OS will start terminating suspended apps that haven't been switched to in a while, without warning. This is why it's in your best interest for your app to remember it's state as well, so if your app is terminated, then re-opened, the user doesn't really notice because it still returns to right where they left off.

2) Your app uses one of the "background" APIs. These are for audio playback, VoIP services, or location services. In this case, your app is allowed to continue running in the background but only has access to those APIs. Additionally, your app can designate certain long-running tasks as "background tasks" that need to be completed before the app is suspended or terminated, like uploading pictures to Flickr or rendering a video, etc.

The "background task" method doesn't cover pinging servers indefinitely, as there is a time limit for the task, after which it will be forcibly halted. Apps that need that sort of functionality are expected to implement push notifications, just as before.

That should clear this up. All in all I think it's a pretty elegant solution to multitasking on a mobile device.

Alex Ford
+1  A: 

It is possible for apps to request background time. Read the docs. I would say it iOS is "controlled multitasking".

Moshe
+1  A: 

You can create a long running background task, I believe these can include networking features. Just have to set the background task flag on the work block.

https://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

the OS can limit exactly how much time you get though... it will tell you when your time is up giving you a chance to cleanup nicely.

William Denniss
It's important that you do as much cleanup as possible *before* you move into the background, though. If the phone is running low on memory and has to kill your app, you don't get any warning at all, you just get killed.
kubi
A: 

iOS 4 has "real" multitasking to some extend. There are two things to consider:

  • The UI event loop is single tasking. So only the front app executes on the UI event loop. Not a big deal: detach your main code form the UI event loop (maybe you need to redesign your app).
  • The OS "may" terminate your app if some criteria are met (e.g. low memory).

Currently one of these criteria is that execution time is limited to 10 minutes (real time not cpu time). However I expect this to change and see better criteria for background app termination (I hope to).

Apart from this you can have timers (event loops) in background.

Christian Fries