Instance Method

scheduleBackgroundRefresh(withPreferredDate:userInfo:scheduledCompletion:)

Schedules a background task to refresh your app’s data.

Declaration

func scheduleBackgroundRefresh(withPreferredDate preferredFireDate: Date, userInfo: NSSecureCoding?, 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 willActivate() and didActivate methods. You can check your extension’s applicationState 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 WKApplicationRefreshBackgroundTask.

See Also

Managing the Execution State

var applicationState: WKApplicationState

The runtime state of the Watch app.

enum WKApplicationState

The running states of your Watch app.

var isApplicationRunningInDock: Bool

A Boolean value that indicates whether the app is running in the dock.

Beta
var isFrontmostTimeoutExtended: Bool

A Boolean value that determines whether the app extends its time as the frontmost app.

Beta