---
title: WKURLSessionRefreshBackgroundTask
framework: watchkit
role: symbol
role_heading: Class
path: watchkit/wkurlsessionrefreshbackgroundtask
---

# WKURLSessionRefreshBackgroundTask

A task that responds to background URL sessions.

## Declaration

```swift
class WKURLSessionRefreshBackgroundTask
```

## Mentioned in

Using background tasks

## Overview

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. note: In watchOS 9 and later, SwiftUI Background tasks are the preferred way to handle background tasks and interactions. For more information, backgroundTask(_:action:). 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

- [sessionIdentifier](watchkit/wkurlsessionrefreshbackgroundtask/sessionidentifier.md)

## Relationships

### Inherits From

- [WKRefreshBackgroundTask](watchkit/wkrefreshbackgroundtask.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

## See Also

### Background tasks

- [Using background tasks](watchkit/using-background-tasks.md)
- [Preparing to take your watchOS app’s snapshot](watchkit/preparing-to-take-your-watchos-app-s-snapshot.md)
- [WKApplicationRefreshBackgroundTask](watchkit/wkapplicationrefreshbackgroundtask.md)
- [WKWatchConnectivityRefreshBackgroundTask](watchkit/wkwatchconnectivityrefreshbackgroundtask.md)
- [WKBluetoothAlertRefreshBackgroundTask](watchkit/wkbluetoothalertrefreshbackgroundtask.md)
- [WKIntentDidRunRefreshBackgroundTask](watchkit/wkintentdidrunrefreshbackgroundtask.md)
- [WKRelevantShortcutRefreshBackgroundTask](watchkit/wkrelevantshortcutrefreshbackgroundtask.md)
- [WKSnapshotRefreshBackgroundTask](watchkit/wksnapshotrefreshbackgroundtask.md)
- [WKRefreshBackgroundTask](watchkit/wkrefreshbackgroundtask.md)
