3

I have the following code in my AppDelegate:

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

        application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)

        return true
    }

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

    completionHandler(UIBackgroundFetchResult.NewData)
    getData()
}

func getData(){

    // function that gets certain data from the server and notifies the 
    // user of the info fetched
}

I am facing two problems here:

  1. the first problem is that the background fetch is never executed unless I click on "Debug>simulate background fetch". i waited for it indefinitely and it was never called on its own.

  2. The second problem is that when i click on simulate background fetch while my app is still open, the simulator exists the app and then performs the background fetch, even though the background fetch should still work even while the application is running and without closing it.

Am I missing any code related to background fetch ? Or is there any wrong code ? This is the only code I have written related directly to background fetch other than adding background fetch to the capabilities.

unknown_111
  • 111
  • 1
  • 3
  • 12

2 Answers2

1

Apple provides an algorithm which defines how often the background fetch should trigger, based on your own usage of the app. If you use it a lot, then it will fetch as often as possible, but if you use like at 4pm every day, the background fetch should trigger just before, so your data is updated when you launch it.

check detailed answer : Background fetch stops working after about 10 to 14 hours

Community
  • 1
  • 1
Mohamed Mostafa
  • 1,057
  • 7
  • 12
  • but if the system of the user's phone decided to trigger the background fetch while the user is using the app, does this close his/her app while they're using it ? biz i thought of "simulate background fetch" similar to the system's execution of it and this is what was happening when i run "simulate background fetch" – unknown_111 Jul 30 '16 at 07:17
  • If the user is using the app, it's not backgrounded so will never be triggered. – Agreensh May 20 '17 at 09:06
0

There is a option in iPhone settings to turn off Background App Refresh, it is responsible for invoking the background fetch and it can also turned off globally for all apps.

Check Settings > General > Background App Refresh

Background App Refresh Settings

SuckLips
  • 55
  • 1
  • 11