6

I'm writing an app for a client where the users will enter data during the day, then at a given point at night (say, at midnight) the user's data for that day should upload to the server.

Therefore I need to schedule a method to run on my app at midnight, even if the app is in the background. This method will then sync the collected data with the server and download any changes.

In order to achieve this, I imagine I need to set the app to always run in the background (i.e. longer than 10 minutes) then schedule the function to run after a specific time. Do I do this by using performSelector: withDelay:? Or do I need something more robust because the app will be in the background?

Thanks guys!

Justin
  • 1,881
  • 4
  • 20
  • 40
theDuncs
  • 4,649
  • 4
  • 39
  • 63

2 Answers2

2

You might check out UILocalNotifications. You could schedule that event to happen and then set a badge icon to let the user know it processed something (or not).

Info on these HERE

LJ Wilson
  • 14,445
  • 5
  • 38
  • 62
  • I don't want the user to know anything about the data sync, so ideally I would (a) schedule a local notification for midnight, then (b) at midnight when the notification code runs I can perform the data sync then NOT raise any notification on the screen. Does this sound feasible? Or to use local notifications do I have to raise an alert on the screen? – theDuncs May 20 '12 at 11:55
  • I think you can do what you are trying to do without a visual indicator to the user. – LJ Wilson May 20 '12 at 12:07
  • From what I can see, the UILocalNotification would be scheduled for midnight, but if the app is in the background at midnight, it would show an alert. The user would have to touch the alert to process the code. Do you know of a way to get the app to run the sync in the background at the point that the LocalNotification is due to be shown to the user? Or once it's in the background is it out of the app's control? – theDuncs May 20 '12 at 12:26
  • As far as I know, the only way to process events once an app is in the background is to use either Local or Push Notifications. – LJ Wilson May 20 '12 at 14:43
0

I don't think your app or what you are trying to do is qualified for Apple's requirement for long running background task. Check out the "Implementing long-running background tasks" section of this doc from Apple.

user523234
  • 14,323
  • 10
  • 62
  • 102
  • That's a good point - but actually the app may qualify for continued location tracking. The reason for the midnight data sync is because during the day the user's are likely to be outside of internet connectivity. So perhaps the best option would be track major location changes, and at each change check for internet connectivity. If the user has a strong enough signal, the sync could begin there and then, then switch off data syncing until the next day. Thanks for the help! – theDuncs May 20 '12 at 12:02