BGTaskScheduler submit call fails with BGTaskSchedulerErrorDomain code: 1

6.4k views Asked by At

BGTaskScheduler fails with "BGTaskSchedulerErrorDomain" - code: 1 on iOS 13.0

I have tried the sample program ColorFeed made available by Apple and it faces the same issue

let request = BGProcessingTaskRequest(identifier: "com.example.apple-samplecode.ColorFeed.db_cleaning")
request.requiresNetworkConnectivity = false
request.requiresExternalPower = true

do 
{
  try BGTaskScheduler.shared.submit(request)
} catch {
  print("Could not schedule database cleaning: \(error)")
}

The above code gets called in applicationDidEnterBackground

Earlier the task is registered in didFinishLaunchingWithOptions ...

 BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.example.apple-samplecode.ColorFeed.db_cleaning", using: nil) { task in

        // Downcast the parameter to a processing task as this identifier is used for a processing request.
        self.handleDatabaseCleaning(task: task as! BGProcessingTask)

 }
2

There are 2 answers

1
ahbou On

In my case I had to run the app in Release mode to get it to work.

Edit Scheme -> Build configuration -> Release

5
Paulw11 On

code 1 indicates BGTaskScheduler.Error.unavailable

According to the documentation:

This error usually occurs for one of three reasons:

  • The user has disabled background refresh in settings.
  • The app is running on Simulator which doesn’t support background processing.
  • The extension either hasn’t set RequestsOpenAccess to YES in The Info.plist File, or the user hasn’t granted open access.

Are you running on a real device? Is background refresh enabled on that device?