FetchWithCompletionHandler never starts

1) My plist configuration provides backgroundmode:

<key>UIBackgroundModes</key> <array> <string>fetch</string> </array> 

2) In didFinishLaunchingWithOptions I:

 [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:1.0]; 

3) I declared the UIApplicationDelegate protocol in the delegate.

4) I applied the following method, but it never starts. (It only works if I simulate a selection using "Xcode-> Debug-> Simulate Background Fetch.")

 -(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 

Why? Is this a DP5 beta bug? Do I have to radar this?

+48
ios objective-c ios7
Aug 19 '13 at 14:02
source share
9 answers

I am afraid that it is difficult to debug on the device, because you are not guaranteed that it will be called within the time specified by you.

setMinimumBackgroundFetchInterval means that it is not called in an interval that is less than the value you specify. But there is no setMaximumBackgroundFetchInterval . Therefore, if iOS decides to call your application only once a day or even once a week, and will not be called more often than your minimumBackgroundFetchInterval . AFAIK iOS decides when to call the performFetchWithCompletionHandler , measured by the template, when and how often users launch the application.

+33
Aug 19 '13 at 14:06 on
source share

In Xcode 5 debugging mode, you can force the background selection from the menu: Debug> Simulate Background Fetch.

Ruff!

+76
Oct 17 '13 at 17:47
source share

(It only works if I simulate a sample using "XCode-> Debug-> Simulate Background Sampling.")

This is because you are in debug mode. Try to run the application without Xcode.

+16
May 23 '14 at 16:21
source share

Using your device, you can run application:performFetchWithCompletionHandler with the following steps:

  • Put the app in the background state
  • Lock the device and wait 5 minutes.
  • Unlock the device, this will call the method
+15
Oct 30 '13 at 13:47
source share

There are many considerations:

  • Make sure the background fetch function is set to plist.

  • Make sure the background fetch feature has not been disabled for this particular application or, in general, in the Device Settings application.

  • Be sure to set the minimum sampling interval.

  • Make sure that you gracefully left the application (for example, just press the "home" button and start another application and / or just lock the device). But if you kill the application (double-clicking on the "home" button and scrolling up or what you have), which will prevent the OS from offering your application the ability to disable subsequent requests to select the background (at least until the user starts the application again).

  • Make sure you test this on a physical device and do not run the application through the Xcode debugger. Binding to the debugger changes the behavior of background operations.

  • Make sure that the application does indeed perform some network requests. If you have an application that does not perform network requests at all, it will not participate in background extraction. If you make, for example, a small test application with "background extraction" and do not issue network requests, you will not participate in background extraction.

    Similarly, if the OS starts your application in the background so that it can perform background extraction, if you are not actually performing a network request, the OS may stop offering your application the ability to perform background settings in the future,

  • Be sure to call the completion handler and do it within the allotted time, or your application may not participate in background extraction in the future.

  • The time during which the OS performs the background selection is dictated by poorly documented rules that may change in the future. But relevant factors include:

    • Is the device connected to power and / or sufficiently charged,

    • Whether connected to Wi-Fi or not;

    • How often does the user launch the application,

    • Is the device other network tasks (for example, can background sampling be combined with other network operations)

    • How often past background fetch requests have led to data availability.


    In my experience, after the application launches for the first time, if connected to Wi-Fi and power, if you wake up the device after about 5 minutes, the application will perform background extraction. This is not a complicated and quick rule, but what we have experienced in the past.

    But many new developers are posting in Qaru with questions such as “how can I receive application request data every x minutes (or hours)”, “how can I request data every day at 2 a.m.”, etc. The short answer is that you cannot. The OS decides the background time at its discretion. You cannot control this (except for the minimum request interval, but you cannot control the maximum interval since the OS controls this).

  • This may seem obvious to many, but make sure you have a reliable way to find out if the background extraction process is working correctly or not. A user notification frame can be used to present some warning so that you know that something has come up as a result of a background request. Alternatively, os_log Unified Notifications can be used to publish messages to a device that can be monitored in the macOS Console application. But more than once I saw that users do something like waiting for a message to appear in Xcode or waiting for a UIAlertController . You need some kind of mechanism that works when it is not connected to Xcode and when the application never comes to the fore.

+8
Jul 10 '17 at 0:18
source share

Another thing to check is your plist file. Verify that the UIApplicationExitsOnSuspend button UIApplicationExitsOnSuspend missing.

Many people here at Stack Overflow have recommended using this option as a way to get your application to run every time it starts. This works, but the side effect is that it prevents the launch of the new iOS 7 fetch fetch feature.

+2
Oct 30 '13 at 21:23
source share

If application: performFetchWithCompletionHandler: never starts (unless you mimic it using Xcode), check also if the preference "Update Background Application" is "On" for your application. (Settings app → General → Background app update)

+2
May 14 '14 at
source share

In addition, background sampling is disabled if the iPhone is in low power mode.

+2
Oct 11 '16 at 1:25
source share

Apple provides an algorithm that determines how often background sampling should run based on your own use of the application. If you use it a lot, it will be extracted as often as possible, but if you use it at 4 p.m. every day, background sampling should start earlier, so your data is updated at startup. Send link

+1
Jun 03 '14 at 11:14
source share



All Articles