submitTaskRequest(_:completionHandler:)
Submits a background task request to be scheduled with a completion handler.
Declaration
func submitTaskRequest(_ taskRequest: BGTaskRequest, completionHandler: @escaping @Sendable ((any Error)?) -> Void)func submitTaskRequest(_ taskRequest: BGTaskRequest) async throwsParameters
- taskRequest:
The task request object representing the parameters of the background task to be scheduled.
- completionHandler:
A block that is called when submission completes. The block receives an optional error parameter:
nilif the task was submitted successfullyAn
NSErrorif submission failed
Discussion
This method asynchronously submits the task request and invokes the completion handler with any errors that occur during submission.
Submitting a task request for an unexecuted task that’s already in the queue replaces the previous task request.
There can be a total of 1 refresh task and 10 processing tasks scheduled at any time. Trying to schedule more tasks will result in an error with code BGTaskScheduler.Error.Code.tooManyPendingTaskRequests.
Common errors include:
BGTaskScheduler.Error.Code.notPermitted: Task identifier not permitted or unsupported resources requested
BGTaskScheduler.Error.Code.tooManyPendingTaskRequests: Too many pending tasks of this type
BGTaskScheduler.Error.Code.unavailable: Background refresh disabled or app not permitted
BGTaskScheduler.Error.Code.immediateRunIneligible: Immediate run not eligible due to system conditions
The completion handler is called on an arbitrary queue.
This method replaces the deprecated submit(_:) method