Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to poll server in background

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.

like image 384
Neil Coffey Avatar asked Oct 27 '10 11:10

Neil Coffey


1 Answers

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.

like image 136
jer Avatar answered Oct 11 '22 13:10

jer