Schedules a background task to refresh your app’s data.
SDK
- watchOS 3.0+
Framework
- Watch
Kit
Declaration
func scheduleBackgroundRefresh(withPreferredDate preferredFireDate: Date, userInfo: (NSSecure Coding & NSObject Protocol)?, scheduledCompletion: @escaping (Error?) -> Void)
Parameters
preferredFireDate
The time of the next background app refresh task. The system makes every effort to wake your app in the background at some point after the scheduled time, but the precise time is not guaranteed.
userInfo
A dictionary of custom information associated with the background app refresh task. Pass
nil
if you do not need to associate any custom data with the task.scheduledCompletion
A block that is called by the system after the background app refresh task has completed. This block takes the following parameter:
- error
If an error occurs, this parameter contains an object describing the error; otherwise, it is
nil
.
Discussion
Call this method to update the contents of your app in the background. Background refresh tasks only trigger when the app is in the background. If the app is still running in the foreground at the scheduled time, the system ignores the task.
When the task is triggered, the system wakes your app in the background and calls your extension delegate’s handle(_:)
method. The system also calls your active interface controller’s will
and did
methods. You can check your extension’s application
during these methods to determine whether your app is running in the foreground or in the background.
Use this task to update your application’s state. Often you use background app refresh tasks to schedule other background tasks (for example, URLSession
background transfers or snapshot updates). You can only schedule one background app refresh task at a time. If a background app refresh task has already been scheduled, scheduling a second task cancels the first. Background app refresh tasks are also budgeted. For more information, see WKApplication
.