Instance Method

beginBackgroundTask(withName:expirationHandler:)

Marks the beginning of a new long-running background task with the specified name.

Declaration

func beginBackgroundTask(withName taskName: String?, expirationHandler handler: (() -> Void)? = nil) -> UIBackgroundTaskIdentifier

Parameters

taskName

The name to display in the debugger when viewing the background task. If you specify nil for this parameter, this method generates a name based on the name of the calling function or method.

handler

A handler to be called shortly before the app’s remaining background time reaches 0. You should use this handler to clean up and mark the end of the background task. Failure to end the task explicitly will result in the termination of the app. The handler is called synchronously on the main thread, blocking the app’s suspension momentarily while the app is notified.

Return Value

A unique identifier for the new background task. You must pass this value to the endBackgroundTask(_:) method to mark the end of this task. This method returns UIBackgroundTaskInvalid if running in the background is not possible.

Discussion

The behavior of this method is identical to the behavior of the beginBackgroundTask(expirationHandler:) method except for the addition of an explicit name for the task.

See Also

Managing Background Execution

var applicationState: UIApplicationState

The runtime state of the app.

var backgroundTimeRemaining: TimeInterval

The amount of time the app has to run in the background.

var backgroundRefreshStatus: UIBackgroundRefreshStatus

The ability of the app to be launched into the background so that it can perform background behaviors.

func setMinimumBackgroundFetchInterval(TimeInterval)

Specifies the minimum amount of time that must elapse between background fetch operations.

func beginBackgroundTask(expirationHandler: (() -> Void)? = nil)

Marks the beginning of a new long-running background task.

func endBackgroundTask(UIBackgroundTaskIdentifier)

Marks the end of a specific long-running background task.