Marks the beginning of a new long-running background task with the specified name.
- iOS 7.0+
- tvOS 9.0+
The name to display in the debugger when viewing the background task. If you specify
nilfor this parameter, this method generates a name based on the name of the calling function or method.
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.
A unique identifier for the new background task. You must pass this value to the
end method to mark the end of this task. This method returns
invalid if running in the background is not possible.
This method lets your app continue to run for a period of time after it transitions to the background. You should call this method at times where leaving a task unfinished might be detrimental to your app’s user experience. For example, your app could call this method to ensure that had enough time to transfer an important file to a remote server or at least attempt to make the transfer and note any errors. You should not use this method simply to keep your app running after it moves to the background.
Each call to this method must be balanced by a matching call to the
end method. Apps running background tasks have a finite amount of time in which to run them. (You can find out how much time is available using the
background property.) If you do not call
end for each task before time expires, the system kills the app. If you provide a block object in the handler parameter, the system calls your handler before time expires to give you a chance to end the task.
You can call this method at any point in your app’s execution. You may also call this method multiple times to mark the beginning of several background tasks that run in parallel. However, each task must be ended separately. You identify a given task using the value returned by this method.