<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.11.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URLSessionStreamTask/write(_:timeout:completionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLSessionStreamTask(im)writeData:timeout:completionHandler:"
  },
  "title" : "write(_:timeout:completionHandler:)"
}
-->

# write(_:timeout:completionHandler:)

Asynchronously writes the specified data to the stream, and calls a handler upon completion.

```
func write(_ data: Data, timeout: TimeInterval, completionHandler: @escaping @Sendable ((any Error)?) -> Void)
```

## Parameters

`data`

The data to be written.

`timeout`

A timeout for writing bytes. If the write is not completed within the specified interval, the write is canceled and the `completionHandler` is called with an error. Pass `0` to prevent a write from timing out.

`completionHandler`

The completion handler to call when all bytes are written, or an error occurs. This handler is executed on the delegate queue.

    This completion handler takes the following parameter:

- `error`: An error object that indicates why the write failed, or `nil` if the write was successful.

## Discussion

There is no guarantee that the remote side of the stream has received all of the written bytes at the time that `completionHandler` is called, only that all of the data has been written to the kernel.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)