---
title: "writeData(for:toFile:options:completionHandler:)"
framework: photos
role: symbol
role_heading: Instance Method
path: "photos/phassetresourcemanager/writedata(for:tofile:options:completionhandler:)"
---

# writeData(for:toFile:options:completionHandler:)

Requests the underlying data for the specified asset resource, to be asynchronously written to a local file.

## Declaration

```swift
func writeData(for resource: PHAssetResource, toFile fileURL: URL, options: PHAssetResourceRequestOptions?, completionHandler: @escaping @Sendable ((any Error)?) -> Void)
```

```swift
func writeData(for resource: PHAssetResource, toFile fileURL: URL, options: PHAssetResourceRequestOptions?) async throws
```

## Parameters

- `resource`: The asset resource for which to request data.
- `fileURL`: A URL identifying the local filename at which to write the asset resource’s data.
- `options`: Options specifying how Photos should handle the request and notify your app of progress. For details, see doc://com.apple.photokit/documentation/Photos/PHAssetResourceRequestOptions.
- `completionHandler`: A block that photos calls after the request has been fulfilled or has failed. The block takes a single parameter:

## Discussion

Discussion When you call this method, Photos begins asynchronously reading the underlying data for the asset resource. Depending on the options you specify and the current state of the asset, Photos may download asset data from the network. While reading (or downloading) asset resource data, Photos progressively writes the data into the specified file. After writing all of the data, or if an error prevents reading all of the data, Photos calls your completionHandler block. note: Photos calls your completionHandler block on an arbitrary serial queue. To update the UI in response to these events, dispatch to the main queue.

## See Also

### Requesting Resources

- [requestData(for:options:dataReceivedHandler:completionHandler:)](photos/phassetresourcemanager/requestdata(for:options:datareceivedhandler:completionhandler:).md)
- [cancelDataRequest(_:)](photos/phassetresourcemanager/canceldatarequest(_:).md)
