Periodic background synchronization
Asked Answered
S

2

13

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?

Stefan answered 30/10, 2016 at 11:29 Comment(0)
C
14

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

Ciri answered 30/10, 2016 at 12:15 Comment(2)
@AkashRajput You get punished by getting less time, i.e. your background fetch is triggered less often.Ciri
For those newly finding this post, the section you want is called "Fetching Small Amounts of Content Opportunistically" at developer.apple.com/library/archive/documentation/iPhone/…Seminal
G
0

You can't. Apple doesn't allow 3rd party apps to have regular background time like that. You'll need to come up with another approach like implementing a silent push notification from your server when new content is available.

As @ekscrypto points out in their comment, you can use Background fetch to load small amounts of data when the system decides to fetch it. However, you don't have any control over when that fetching takes place. Search on "Fetching Small Amounts of Content Opportunistically" in the Xcode help system for more information.

Garold answered 30/10, 2016 at 11:34 Comment(4)
WOW! You serious about that? That sounds unbelievable for me to not have such a feature. What about the NSBackgroundActivityScheduler I found it on google but in Swift 3.0 it is not existing.Stefan
You can see from the documentation - developer.apple.com/reference/foundation/… - that NSBackgroundActivityScheduler is for MacOS, not iOS. On iOS you need to use background fetch or server pushMotet
You definitely can, look at developer.apple.com/library/archive/documentation/iPhone/… in a section titled "Fetching Small Amounts of Content Opportunistically"Seminal
@ekscrypto, good point about the opportunistic fetching of data, but that doesn't meet the OPs requirement of fetching the data on regular intervals. Instead, the system decides when would be a good time to fetch data (My guess is when either the cell or WiFi connection is "lit up" and there is live access to the internet.)Garold

© 2022 - 2024 — McMap. All rights reserved.