0

In a test device that's never been used for development, iOS Background Fetch works fine, continuing during the night.

But on two other devices, used for development, the fetches sometimes occur, but often don't, not even during the night.

Did anyone experience the same odd behaviour? Do fetches occur only when there's Wi-Fi available, or are there other factors I might be missing that influences fetch behaviour?

Sneha
  • 2,200
  • 6
  • 22
  • 36
meaning-matters
  • 21,929
  • 10
  • 82
  • 142

2 Answers2

1

Enabling this mode is not a guarantee that the system will give your app any time to perform background fetches. The system must balance your app’s need to fetch content with the needs of other apps and the system itself. After assessing that information, the system gives time to apps when there are good opportunities to do so.

Also please confirm you are using NSURLSession

When downloading any content, it is recommended that you use the NSURLSession class to initiate and manage your downloads. For information about how to use this class to manage upload and download tasks, see URL Session Programming Guide.

https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

Emel Elias
  • 572
  • 7
  • 18
  • Any idea why `NSURLSession`? – meaning-matters Feb 09 '17 at 13:50
  • The most immediate improvement NSURLSession provides over NSURLConnection is the ability to configure per-session cache, protocol, cookie, and credential policies, rather than sharing them across the app. This allows the networking infrastructure of frameworks and parts of the app to operate independently, without interfering with one another. The advantage of this is can perform tasks in background independently detail refer https://www.objc.io/issues/5-ios7/from-nsurlconnection-to-nsurlsession/ – Emel Elias Feb 09 '17 at 15:48
  • I'm using AFNetworking 3.x using `NSURLSession`. That was not the issue, see my answer. Thanks you for your time! – meaning-matters Feb 09 '17 at 22:26
0

Turned out to be the following: I had data protection switched on which required the iPhone to be unlocked before my web credentials could be read.

Before doing the background fetch, I checked if the app was logged in by checking these credentials. This check failed because the keychain was locked.

The two iPhones for which background fetch worked only sporadically had passcode protection enabled, while the iPhone for which it always worked had passcode protection disabled.

I hope this helps other people. Here's a good article discussing my issue, which I found after I painfully sorted it out myself.

meaning-matters
  • 21,929
  • 10
  • 82
  • 142