Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Periodic background synchronization

Im quite new to iOS programming and now want to implement a periodic background synchronization to synchronize my server data with client data. What I want to achieve is comparable with Androids SyncAdapter where you can define a time interval (for example each 30 minutes) and the system will trigger the defined task automatically in the background.

Until now I could not find such mechanism for Swift 3.0 so I need to ask if somone has experience or some hints for me how I can achieve this.

What I want to do sounds quite simple:

When the app starts for the first time the app should setup a sync manager which automatically triggers a background task every 30 minutes. The background task is responsible to synchronize server and client data (using Alamofire).

How can I do that?

like image 456
Mulgard Avatar asked Oct 30 '16 11:10

Mulgard


People also ask

What is disable background sync?

By doing this, apps will not sync in the background and will not send notifications until you open them. Disabling background sync can be a major benefit to battery life, but still allows phone calls to be received when your screen is off. This is a great alternative to Life Mode for many users.

Do service workers run when browser is closed?

Service workers have a total control over every request made from a website. Since they run in the background, they don't need the webpage to be open. That gives them ability to listen and respond to events such as push messages sent from the server to the browser.

What is background fetch?

The Background Fetch API provides a method for managing downloads that may take a significant amount of time such as movies, audio files, and software.


1 Answers

There is an iOS feature called BackgroundFetch, which you can set up for

regularly downloads and processes small amounts of content from the network

You can setup a minimumBackgroundFetchInterval.

In contrast to the mentioned Android feature, this interval is not guaranteed though.

The OS does some heuristic in a blackbox. It rewards you for using a "reasonable" (to the OS) CPU time/ power consumption and also for being used often by the user. On the other hand you get punished for draining the battery or (even worse) never being used/opened by the user.

See: Apple Sample and Apple Docs


Update: Since iOS13, BackgroundFetchis deprecated.

There is a similar, new API named BGTask, BGAppRefreshTask is the equivalent to deprecated BackgroundFetch.

See Apple Docs


Alternatively, depending on your needs, you can post a Silent (push) Notification whenever the users data changes on server side. A silent push wakes up your app without notifying the user, so you can fetch data and maybe inform the user by scheduling a local notification.

See: Apple Documentation

like image 183
shallowThought Avatar answered Dec 07 '22 10:12

shallowThought