Mulgard Mulgard - 1 month ago 23
iOS Question

Swift 3.0 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?

Answer

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

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, even worse if your app is never been used.

See: Apple Documentation


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

Comments