Correct way to poll server in background
Asked Answered
F

2

17

Assuming this is possible, I would like my iOS application, when backgrounded, to poll a server (i.e. essentially, retrieve the contents of a URL every 30 minutes and notify the user if it contains something "interesting"), essentially in a similar way to the way the built-in mail client assumedly works if you're not using push notifications.

Now, from my reading so far (I'm an experienced programmer, but new to iOS), I think there may be two potential ways to do this:

  • Method 1: In applicationDidEnterBackground:, start a background task which does the periodic polling;
  • Method 2: Send your own app a UILocalNotification with no visible text to the user, but which simply serves to wake your app up in X minutes time to do the polling (and then send itself another notification for next tim etc).

I see in Apple's documentation of Scheduling, Registering, and Handling Notifications, they actually seem to have an example usign Method 1 (their "chat" example, Listing 2-2). But what is surprising about this method is that it appears to just sit in a continual loop doing the polling, with no intervening sleep; on platforms I'm more familiar with, this would be inadvisable and would burn CPU.

So subparts of my question are essentially: - Is Method 2 possible (or must a UILocalNotification always cause a visible alert to the user, which is not what I want) and if so is it the recommended way to do this? - If the way to do it is Method 1, is Apple's "chat" example of sitting in a continual loop actually OK (e.g. does iOS ration the CPU so that this isn't an issue), and if not what is the way in iOS to tell the background process to "sleep for X seconds/minutes"? And if Apple's continuous loop is OK for whatever reason, what would then be the way to time the intervals between polling?

N.B. I appreciate that being able to run in the background at all is essentially an iOS 4 feature. I don't mind if my app will only run in iOS 4.

Felid answered 27/10, 2010 at 11:46 Comment(0)
E
14

What you want to do, is not covered under the multitasking features of iOS4. There are only a few types of applications allowed to run in the background for long periods of time (more than 10 minutes), and generic networking applications aren't one of them.

However, all is not lost. What you can do, and what I believe you should do is to use push notifications. On your server, you link up with apple's push notification service, the user registers for push notifications, and you either know what "interesting data" is, or they tell you. When that data is available, you send it to the user immediately via a push notification.

It provides a nicer user experience, and your app doesn't need to be running in the background. iOS will handle delivery of the push notification. If they swipe to unlock the phone when they get the notification, your app will open up, and at which time, you can load that useful information in your app.

Your method 1 won't work long term, even if you do manage to pause input for a while, and this is why: Launching a background task runs a task for NO MORE than 10 minutes, unless you are one of the three types of applications that is allowed to stay running. After 10 minutes, the OS will suspend you.

Your method 2 won't work at all. All local notifications present an alert view to users.

Empanel answered 27/10, 2010 at 12:10 Comment(2)
Thanks -- this does fit in with what I've read (unfortunately). I wonder why Apple show an example of polling a chat server in the background in that case. The main issue with push notifications for me is that I wanted to keep client's confidential login information on the client, but it's sounding like this isn't possible. Interestingly, the iPhone's built in mail client is allowed to break this rule, then?Felid
Use a different authentication/authorization mechanism. Things like OAuth or whatnot. Secondly, I'm not sure which example you're pointing to, but the main thing it might show is location based services in the background, and the 'chat' client. I don't know.Empanel
R
3

Assuming this is possible...

Unfortunately that's a faulty assumption(!).

You can't do it using the "multi tasking" on the iPhone, as it only allows certain kinds of background processing (GPS, VoIP, music). To do what you want you'll need to do the work on a server and use push notifications.

Roter answered 27/10, 2010 at 12:13 Comment(2)
is there any apple documentation that mentions this guideline ?Geophagy
@Geophagy It's not really a guideline, more the services that the public APIs provide. In any case, this was all correct in 2010. These days there are a number of new APIs for background processing.Roter

© 2022 - 2024 — McMap. All rights reserved.