WKURLSessionRefreshBackgroundTask
A task that responds to background URL sessions.
Declaration
class WKURLSessionRefreshBackgroundTaskMentioned in
Overview
Always upload and download data using a URLSession background transfer. Background transfers occur in a separate process and continue to transfer data even after your app terminates. Asynchronous uploads and downloads, on the other hand, suspend with your app. Because watchOS apps have a short runtime, you can’t guarantee that an asynchronous transfer finishes before the app suspends. For more information on background transfers, see Downloading files in the background.
Schedule a background URL session to download an item as shown below.
// Create a background session configuration.
let config = URLSessionConfiguration.background(withIdentifier: myRequestID)
config.isDiscretionary = true
config.sessionSendsLaunchEvents = true
// Create the background download task and schedule it to run in 15 minutes.
let urlSession = URLSession(configuration: config,
delegate: self,
delegateQueue: nil)
let backgroundTask = urlSession.downloadTask(with: url)
backgroundTask.earliestBeginDate = Date().addingTimeInterval(15 * 60)
// Run the task.
backgroundTask.resume()If your app has a complication on the active watch face, it can receive up to four WKURLSessionRefreshBackgroundTask tasks per hour. To avoid throttling, use the earliestBeginDate property to schedule background URL session tasks no closer than 15 minutes apart.
Don’t subclass or create instances of this class. Instead, the system instantiates a WKURLSessionRefreshBackgroundTask object and passes the task object to your extension delegate’s handle(_:) method in response to URLSession events. Defer calling the background URLSession task’s setTaskCompleted() method until all the delegate method calls finish processing.
The system creates a background URLSession task when any of the following events occur:
The server requires authentication to complete a background transfer.
All background transfers associated with a session identifier complete (either successfully or unsuccessfully).
To get more information about the transfer, create a background configuration object with the same session identifier. Next, create a session object using the configuration object and a session delegate. The system automatically associates the new session with the transfer and calls the appropriate delegate methods.
Topics
Accessing background task data
See Also
Background tasks
Using background tasksPreparing to take your watchOS app’s snapshotWKApplicationRefreshBackgroundTaskWKWatchConnectivityRefreshBackgroundTaskWKBluetoothAlertRefreshBackgroundTaskWKIntentDidRunRefreshBackgroundTaskWKRelevantShortcutRefreshBackgroundTaskWKSnapshotRefreshBackgroundTaskWKRefreshBackgroundTask