1

I have an application with background fetch capability. It was one of the first features implemented, and it was working like a charm - calling performFetchWithCompletionHandler every specified time interval (10 minutes).

Later in development, I noticed that this app does not performFetch at all - it was left running for weekend, and it did not fetch even once, while preferred interval was set as 10 minutes. There was no changes to any of the related code, and I checked that it is set up correctly:

  • "background fetch" background mode - check
  • call setMinimumBackgroundFetchInterval - check (straight in appDidFinishLaunching, 10 * 60)
  • implement performFetchWithCompletionHandler - check

I can trigger fetch via Xcode debug tools, but there is no single call to app while left alone. I had checked that iOS setting for background refresh is set to "ON" - both global setting (wifi and cellular) and per-app setting. I did try reinstalling application, changing appID, even purging iPhone completely - nothing did not help.

The most intriguing part is that background fetch in the same build is still working for some testers. All of them have the same latest build, but most of them have no background fetch, while some are still updating in background. I did not notice any correlation with iOS version, device family, test flight or local build variants or something like that:

  • iPhone 11 Pro, iOS 13.2.3, TestFlight - works
  • iPhone 11, iOS 13.3.1, TF - does not work
  • iPhone 8+, iOS 13.2, TF - works
  • iPhone SE, iOS 13.3.1, Local - does not work
  • iPhone 6, iOS 12.4.5, TF - does not work

My next step was to try reverting changes since last build with successful background fetch. Most notable change that happened in that time, was implementation of bundled WatchOS companion app - but how can that break background fetch? Anyway, reverting source code did not help either. Even more - I did try to create a simple background fetch example application, pure Xcode template with only three changes for background fetch (mentioned early) - that did not work too.

I am aware, that there is BackgroundTasks API now in iOS - but unfortunately, I have to support older devices with iOS 12 at best. Still, I did try that API too, using BGAppRefreshTask - that did not work too, but I suppose, it has the same underlying logic as old background fetch.

App does not consume a lot of resources - it has around 15 Mb RAM footprint on average, and execution time is at most 5 seconds - only one file download request with timeout of 10 seconds - just in case. App is not terminated because of resources pressure, or because user force-quit it. Also, I made sure completion handler is called in time, right after download ends.

The question is: Am I really missing some step from background fetch implementation? Is it possible to break environment to this extent? If so, how can I find out what is causing all these issues? Or: it is OK to not have a single bgFetch in weeks? (at this moment some devices did not bgFetch in weeks)

Daenim
  • 19
  • 2
  • BG fetches aren't guaranteed by iOS, you can't rely on them being run. Calling `setMinimumBackgroundFetchInterval` is just a hint to the OS, nothing more. – Gereon Mar 12 '20 at 20:56
  • I'm aware of that, ```Minimum..Interval``` is a good hint, but not a single call in days? And furthermore - it was working fine before, and it is still working for some devices. – Daenim Mar 13 '20 at 15:44
  • This may sound dumb, but did you make sure that the device setting for background updates is in fact on? – Gereon Mar 13 '20 at 16:39
  • Signing & Capabilities -> Background Modes -> Background fetch is on. – Daenim Mar 13 '20 at 18:54
  • No, I'm talking about the setting on the phones themselves. Should be Settings > General > Background Refresh. Users can turn it off globally and per app. – Gereon Mar 13 '20 at 19:05
  • Global is on - for both wi-fi and cellular. Per app is on too. – Daenim Mar 13 '20 at 19:21
  • Last thing I can think of: make sure to tell your testers that they shouldn't be force-quitting your app (via the task manager). Apps killed in this way are usually not relaunched in the background until the user next starts them. – Gereon Mar 13 '20 at 19:55
  • I'm actually one of those who can not get bgFetches. So I'm sure that I do not force-quit application - I just press "Home" after application has launched and is set up. And I can see that app is still in backround - debug session is active and I can trigger bgFetch manually via debug tools. – Daenim Mar 13 '20 at 20:04

0 Answers0