1

I need to execute particular task when application state is background/terminated. Task has to fetch data from CoreData table and form a JSON and then Sync to server using REST API Call using URLSession.shared.

I have precisely followed the documentation from Apple and couple of blogs to setup background job from Coding & Xcode configurations perspective.

I have set time-interval to 1 minute after App goes into background for BGAppRefreshTask.

request.earliestBeginDate = Date(timeIntervalSinceNow: 60)

Knowing the fact that, iOS system has many parameters to meet to execute background fetch. I waited for 24 hours for application to run the background job, but didn't executed job for single time even.

Please share, If anybody has experienced the same and come out with particular solution or justification.

Reference links which I followed.

https://www.andyibanez.com/posts/modern-background-tasks-ios13/

https://medium.com/snowdog-labs/managing-background-tasks-with-new-task-scheduler-in-ios-13-aaabdac0d95b

Rashesh Bosamiya
  • 609
  • 1
  • 9
  • 13
  • Same problem here. I got it work for a week. But it’s not working anymore after I add the second task to the whitelist. It’s submitted and registered well. Somehow the system just don’t trigger these tasks. I tried to use a command in Xcode to simulate running, and it’s done well. – Kimi Chiu May 15 '20 at 01:19

1 Answers1

2

URLSession.shared is explicitly documented as

You can’t perform background downloads or uploads when your app isn’t running.

Create a session suitable for background processing instead:

let config = URLSessionConfiguration.background(withIdentifier: "foo")
let session = URLSession(configuration: config)
Gereon
  • 17,258
  • 4
  • 42
  • 73
  • Thank you for your suggestion. However, I have logged a timestamp in UserDefaults as soon as Background Job is executed. But actually job is not running. Though, I will implement your suggestion for REST API Call. – Rashesh Bosamiya Mar 16 '20 at 07:44