requestData(for:options:dataReceivedHandler:completionHandler:)
Requests the underlying data for the specified asset resource, to be delivered asynchronously.
Declaration
func requestData(for resource: PHAssetResource, options: PHAssetResourceRequestOptions?, dataReceivedHandler handler: @escaping (Data) -> Void, completionHandler: @escaping ((any Error)?) -> Void) -> PHAssetResourceDataRequestIDParameters
- resource:
The asset resource for which to request data.
- options:
Options specifying how Photos should handle the request and notify your app of progress. For details, see Phassetresourcerequestoptions.
- handler:
A block that Photos calls to provide the requested data.
The block takes a single parameter:
- data
The requested asset resource data.
- completionHandler:
A block that photos calls after the request has been fulfilled or has failed.
The block takes a single parameter:
- error
If the request has failed, an
NSErrorobject describing the failure; otherwisenil.
Return Value
A numeric identifier for the request. If you need to cancel the request before it completes, pass this identifier to the cancelDataRequest(_:) method.
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 calls your handler block at least once, progressively providing chunks of data. After reading all of the data, Photos calls your completionHandler block to indicate that the data is complete. (At this point, the complete data for the asset is the concatenation of the data parameters from all calls to your handler block.) If Photos cannot finish reading or downloading asset resource data, it calls your completionHandler block with a description of the error. Photos can also call the completionHandler block with a non-nil error when the data is complete if the user cancels downloading.