---
title: "readData(ofMinLength:maxLength:timeout:completionHandler:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/urlsessionstreamtask/readdata(ofminlength:maxlength:timeout:completionhandler:)"
---

# readData(ofMinLength:maxLength:timeout:completionHandler:)

Asynchronously reads a number of bytes from the stream, and calls a handler upon completion.

## Declaration

```swift
func readData(ofMinLength minBytes: Int, maxLength maxBytes: Int, timeout: TimeInterval, completionHandler: @escaping @Sendable (Data?, Bool, (any Error)?) -> Void)
```

```swift
func readData(ofMinLength minBytes: Int, maxLength maxBytes: Int, timeout: TimeInterval) async throws -> (Data?, Bool)
```

## Parameters

- `minBytes`: The minimum number of bytes to read.
- `maxBytes`: The maximum number of bytes to read.
- `timeout`: A timeout for reading bytes. If the read is not completed within the specified interval, the read is canceled and the completionHandler is called with an error. Pass 0 to prevent a read from timing out.
- `completionHandler`: The completion handler to call when all bytes are read, or an error occurs. This handler is executed on the delegate queue. This completion handler takes the following parameters:

## See Also

### Reading and writing data

- [write(_:timeout:completionHandler:)](foundation/urlsessionstreamtask/write(_:timeout:completionhandler:).md)
