<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "HealthKit",
  "identifier" : "/documentation/HealthKit/HKAttachmentStore/streamData(for:dataHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "HealthKit"
    ],
    "preciseIdentifier" : "c:objc(cs)HKAttachmentStore(im)streamDataForAttachment:dataHandler:"
  },
  "title" : "streamData(for:dataHandler:)"
}
-->

# streamData(for:dataHandler:)

Asynchronously returns the attachment’s data.

```
func streamData(for attachment: HKAttachment, dataHandler: @escaping (Data?, (any Error)?, Bool) -> Void) -> Progress
```

## Parameters

`attachment`

An attachment associated with an object in the HealthKit store.

`dataHandler`

A closure that the system calls repeatedly to return the attachment’s contents. This closure takes the following parameters:

- dataChunk: A data object that contains the next chunk of the attachment’s contents. If an error occurred, the system sets this property to `nil`.
- error: If an error occurred, this parameter contains information about the error. Otherwise, it’s `nil`.
- done: A Boolean value that indicates whether the transfer is complete. If this is the last `dataChunk`, the system sets this property to <doc://com.apple.documentation/documentation/Swift/true>.

## Discussion

Call this method to incrementally read the attachment’s contents directly from the attachment store.

```swift
var data = Data()
attachmentStore.streamData(for: myAttachment) { dataChunk, error, done in
    
    if let error {
        // Handle the error here.
        fatalError("*** An error occurred while streaming the attachment's data. \(error.localizedDescription) ***")
    }
    
    guard let dataChunk else { return }
    
    data.append(dataChunk)
    
    if done {
        // Use the attachment's data here.
        print(data)
    }
}
```

---

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)