---
title: URLSessionTask
framework: foundation
role: symbol
role_heading: Class
path: foundation/urlsessiontask
---

# URLSessionTask

A task, like downloading a specific resource, performed in a URL session.

## Declaration

```swift
class URLSessionTask
```

## Mentioned in

Uploading data to a website Uploading streams of data

## Overview

Overview The URLSessionTask class is the base class for tasks in a URL session. Tasks are always part of a session; you create a task by calling one of the task creation methods on a URLSession instance. The method you call determines the type of task. Use URLSession‘s dataTask(with:) and related methods to create URLSessionDataTask instances. Data tasks request a resource, returning the server’s response as one or more NSData objects in memory. They are supported in default, ephemeral, and shared sessions, but are not supported in background sessions. Use URLSession‘s uploadTask(with:from:) and related methods to create URLSessionUploadTask instances. Upload tasks are like data tasks, except that they make it easier to provide a request body so you can upload data before retrieving the server’s response. Additionally, upload tasks are supported in background sessions. Use URLSession’s downloadTask(with:) and related methods to create URLSessionDownloadTask instances. Download tasks download a resource directly to a file on disk. Download tasks are supported in any type of session. Use URLSession’s streamTask(withHostName:port:) or streamTask(with:) to create URLSessionStreamTask instances. Stream tasks establish a TCP/IP connection from a host name and port or a net service object. After you create a task, you start it by calling its resume() method. The session then maintains a strong reference to the task until the request finishes or fails; you don’t need to maintain a reference to the task unless it’s useful for your app’s internal bookkeeping. note: All task properties support key-value observing.

## Topics

### Controlling the task state

- [cancel()](foundation/urlsessiontask/cancel().md)
- [resume()](foundation/urlsessiontask/resume().md)
- [suspend()](foundation/urlsessiontask/suspend().md)
- [state](foundation/urlsessiontask/state-swift.property.md)
- [URLSessionTask.State](foundation/urlsessiontask/state-swift.enum.md)
- [priority](foundation/urlsessiontask/priority.md)
- [URL session task priority](foundation/url-session-task-priority.md)

### Obtaining task progress

- [progress](foundation/urlsessiontask/progress.md)
- [countOfBytesExpectedToReceive](foundation/urlsessiontask/countofbytesexpectedtoreceive.md)
- [countOfBytesReceived](foundation/urlsessiontask/countofbytesreceived.md)
- [countOfBytesExpectedToSend](foundation/urlsessiontask/countofbytesexpectedtosend.md)
- [countOfBytesSent](foundation/urlsessiontask/countofbytessent.md)
- [NSURLSessionTransferSizeUnknown](foundation/nsurlsessiontransfersizeunknown.md)

### Obtaining general task information

- [currentRequest](foundation/urlsessiontask/currentrequest.md)
- [originalRequest](foundation/urlsessiontask/originalrequest.md)
- [response](foundation/urlsessiontask/response.md)
- [taskDescription](foundation/urlsessiontask/taskdescription.md)
- [taskIdentifier](foundation/urlsessiontask/taskidentifier.md)
- [error](foundation/urlsessiontask/error.md)

### Determining task behavior

- [prefersIncrementalDelivery](foundation/urlsessiontask/prefersincrementaldelivery.md)

### Using a task-specific delegate

- [delegate](foundation/urlsessiontask/delegate.md)
- [URLSessionTaskDelegate](foundation/urlsessiontaskdelegate.md)

### Scheduling tasks

- [countOfBytesClientExpectsToReceive](foundation/urlsessiontask/countofbytesclientexpectstoreceive.md)
- [countOfBytesClientExpectsToSend](foundation/urlsessiontask/countofbytesclientexpectstosend.md)
- [NSURLSessionTransferSizeUnknown](foundation/nsurlsessiontransfersizeunknown.md)
- [earliestBeginDate](foundation/urlsessiontask/earliestbegindate.md)

### Deprecated

- [init()](foundation/urlsessiontask/init().md)
- [new()](foundation/urlsessiontask/new().md)

## Relationships

### Inherits From

- [NSObject](objectivec/nsobject-swift.class.md)

### Inherited By

- [URLSessionDataTask](foundation/urlsessiondatatask.md)
- [URLSessionDownloadTask](foundation/urlsessiondownloadtask.md)
- [URLSessionStreamTask](foundation/urlsessionstreamtask.md)
- [URLSessionWebSocketTask](foundation/urlsessionwebsockettask.md)

### Conforms To

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

## See Also

### Essentials

- [Fetching website data into memory](foundation/fetching-website-data-into-memory.md)
- [Analyzing HTTP traffic with Instruments](foundation/analyzing-http-traffic-with-instruments.md)
- [URLSession](foundation/urlsession.md)
