Best way to wake app to sync data

If I have data on server that is updated periodically (e.g., daily or weekly), and I want to have that data sync'd to iOS app, what is best way? I want to make sure it will get updated when app is in foreground, background, swiped away (terminated), or following a phone restart. The two ways I'm thinking of are PushKit and User Notifications.

I'm currently using User Notifications, but I am finding that sometimes the notification doesn't get processed (haven't yet determined reliable conditions for duplicating this problem).

I'm mainly wondering if, given the use case (occasional updates that ideally are processed in background ASAP), this is the best approach.

The best way to sync data, especially if your data is updating weekly, would be to use the Background Tasks Framework. This would allow you to loosely schedule a repeating task which you can use to sync your data.

The sample project Refreshing and Maintaining Your App Using Background Tasks shows how to use scheduled background tasks for refreshing your app content.

The two caveats with this are:

1- you cannot set a specific date/time for the task. It will run whenever the system considers is appropriate.

2- it will not work if the app is swiped away (which is true for most methods that will execute in the background) until the user relaunches the app themselves.

If you are using push notifications, I presume you are using content-available:1 in your payload, and is likely silent pushes. While these kinds of pushes are designed exactly for your purpose of refreshing data, they are not guaranteed to execute your app every single time. These are heavily throttled, and will execute your app only when the system thinks is appropriate (based on device state, and many factors). While you can expect 1-2 content pushes to reach your app per hour, none may, depending on these many factors. Also, if the user swipes away the app, then these notifications will never reach your app until it is relaunched by the user again.

If, for some reason, it is important to process the updated data ASAP upon change and on demand, one other option you have is to use a [Notification Service Extension] (https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension.)

The Notification Service Extension will be executed for every visible push notification. So, it could serve your needs, as long as the user has not disabled the visibility of your notifications through various settings, and your users won't be annoyed by seeing these notifications.

The service extension will not be executed for push notifications that will not be presented visually. So, if you chose to go down this route, you will need to find a way to make these notifications not disturbing as much as you can.


Argun Tekant /  DTS Engineer / Core Technologies

The sample project Refreshing and Maintaining Your App Using Background Tasks shows how to use scheduled background tasks for refreshing your app content.

As a side comment, make sure you pay attention to EXACTLY that sample does AND when/how it does it. From helping MANY developers, the most common issue is that they create a very straight forward implementation without ever looking at the sample or really thinking about exactly what their code is going to do.

The standard example of this is that there code sets up it's processing tasks in applicationDidFinishLaunching by doing the following:

  1. Sets up BGTaskScheduler
  2. cancels all existing tasks to clear things out.
  3. Schedules one or more BGProcessingTasks to run an hour from now.

The direct result of that logic is that "BGProcessingTasks" NEVER run. More specifically, what ends up happening:

  • At some point "during the day" there app is terminated by the system for "normal" reasons (device reboot, free up memory, etc.).

  • Processing time arrives so the system launches their app into the background.

  • Their app launches, cancels all task, and schedules them to run "in the future".

  • The system notices that they no longer have any eligible tasks, so it suspends the app again.

The issue is obvious once you're paying attention to what your app is actually doing, but I've seen many developers make this mistake. Make sure you've thought through what you're actually telling the API to "do".

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

Argun and Kevin,

Thank you both for your suggestions and explanations. The main use case is periodically running the refresh in the background. There are very few cases where the user actually interacts with the app UI, and they are probably executed very infrequently. So my guess is that the user will frequently swipe the app away after using it, but I still need the background processes to run. It sounds like neither background tasks nor silent push notifications can help me with this.

Do I have any other options?

Best way to wake app to sync data
 
 
Q