0

My app want to update server about users location after every 5 seconds even if app is in background. I implemented Background fetch for it.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {        
        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil))

        application.setMinimumBackgroundFetchInterval(5.0)

        return true
    }

func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

    completionHandler(UIBackgroundFetchResult.NewData)
    UpdateData()
}

func UpdateData(){
     print("UpdateData executes")
    // function to update data when ever this method triggers
}

but the problem is

  1. I am unable to enter performFetchWithCompletionHandler method unless I click Debug>simulate background fetch
  2. How can I achieve to invoke performFetchWithCompletionHandler after every 5 seconds.
Azhar
  • 20,500
  • 38
  • 146
  • 211

1 Answers1

5

You have a misunderstanding about what background fetch is. Developers have no power over when a background fetch will be performed exactly. iOS itself decides when to allow an app to perform a background fetch.

The setMinimumBackgroundFetchInterval function only allows you to specify a minimum time interval that has to pass between background fetches to minimize the energy and data usage of your app. However, the interval you set here does not guarantee at all that your app will be able to perform a background fetch this frequently. The key sentence in the documentation regarding this is "Fetch content opportunistically in the background...".

At the moment, the only way to ensure that your app can execute certain functions (including fetching data from a server) in the background is by sending silent push notifications from your own server at regular intervals. However, even then, the system might decide not to wake up your app in response to a silent push notification if your app takes to long to finish execution in response to the push or if your app receives too many notifications.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • ok ... can it be done in Location updates background mode ... to set location get interval in background and as soon as this interval method/delegate get call, send message to server with latest location after interval of 5 sec. – Azhar May 16 '18 at 19:11
  • @Azhar you could try using [deferred location updates](https://developer.apple.com/documentation/corelocation/cllocationmanager/1620547-allowdeferredlocationupdates), which is the only way to set a time interval for calling the delegate periodically (be aware that the GPS will still be on for the full duration, it won't just be switched on every 5mins in your case). Moreover, the system might not give enough execution time for your app to finish a background `URLRequest`. I really wouldn't recommend abusing the location updates for regular background network requests. – Dávid Pásztor May 16 '18 at 19:18
  • 1
    Firstly, your users will experience seriously degraded battery life due to the constant GPS usage of your app and secondly Apple might reject your app for misusing background location updates. – Dávid Pásztor May 16 '18 at 19:19
  • ye, I have already communicated to client that it could be harmful for battery life and Apple could enforce to put such a text in app description ... but he insisted to do it at any cost :) – Azhar May 17 '18 at 01:22