DownloadResponse

public struct DownloadResponse<Value>
extension DownloadResponse: CustomStringConvertible, CustomDebugStringConvertible

Used to store all data associated with a serialized response of a download request.

  • The URL request sent to the server.

    Declaration

    Swift

    public let request: URLRequest?
  • The server’s response to the URL request.

    Declaration

    Swift

    public let response: HTTPURLResponse?
  • The temporary destination URL of the data returned from the server.

    Declaration

    Swift

    public let temporaryURL: URL?
  • The final destination URL of the data returned from the server if it was moved.

    Declaration

    Swift

    public let destinationURL: URL?
  • The resume data generated if the request was cancelled.

    Declaration

    Swift

    public let resumeData: Data?
  • The result of response serialization.

    Declaration

    Swift

    public let result: Result<Value>
  • The timeline of the complete lifecycle of the request.

    Declaration

    Swift

    public let timeline: Timeline
  • Returns the associated value of the result if it is a success, nil otherwise.

    Declaration

    Swift

    public var value: Value? { get }
  • Returns the associated error value if the result if it is a failure, nil otherwise.

    Declaration

    Swift

    public var error: Error? { get }
  • Creates a DownloadResponse instance with the specified parameters derived from response serialization.

    Declaration

    Swift

    public init(
    	request: URLRequest?,
    	response: HTTPURLResponse?,
    	temporaryURL: URL?,
    	destinationURL: URL?,
    	resumeData: Data?,
    	result: Result<Value>,
    	timeline: Timeline = Timeline())

    Parameters

    request

    The URL request sent to the server.

    response

    The server’s response to the URL request.

    temporaryURL

    The temporary destination URL of the data returned from the server.

    destinationURL

    The final destination URL of the data returned from the server if it was moved.

    resumeData

    The resume data generated if the request was cancelled.

    result

    The result of response serialization.

    timeline

    The timeline of the complete lifecycle of the Request. Defaults to Timeline().

    Return Value

    The new DownloadResponse instance.

  • The textual representation used when written to an output stream, which includes whether the result was a success or failure.

    Declaration

    Swift

    public var description: String { get }
  • The debug textual representation used when written to an output stream, which includes the URL request, the URL response, the temporary and destination URLs, the resume data, the response serialization result and the timeline.

    Declaration

    Swift

    public var debugDescription: String { get }
  • Evaluates the given closure when the result of this DownloadResponse is a success, passing the unwrapped result value as a parameter.

    Use the map method with a closure that does not throw. For example:

    let possibleData: DownloadResponse<Data> = ...
    let possibleInt = possibleData.map { $0.count }
    

    Declaration

    Swift

    public func map<T>(_ transform: (Value) -> T) -> DownloadResponse<T>

    Parameters

    transform

    A closure that takes the success value of the instance’s result.

    Return Value

    A DownloadResponse whose result wraps the value returned by the given closure. If this instance’s result is a failure, returns a response wrapping the same failure.

  • Evaluates the given closure when the result of this DownloadResponse is a success, passing the unwrapped result value as a parameter.

    Use the flatMap method with a closure that may throw an error. For example:

    let possibleData: DownloadResponse<Data> = ...
    let possibleObject = possibleData.flatMap {
        try JSONSerialization.jsonObject(with: $0)
    }
    

    Declaration

    Swift

    public func flatMap<T>(_ transform: (Value) throws -> T) -> DownloadResponse<T>

    Parameters

    transform

    A closure that takes the success value of the instance’s result.

    Return Value

    A success or failure DownloadResponse depending on the result of the given closure. If this instance’s result is a failure, returns the same failure.

  • Evaluates the specified closure when the DownloadResponse is a failure, passing the unwrapped error as a parameter.

    Use the mapError function with a closure that does not throw. For example:

    let possibleData: DownloadResponse<Data> = ...
    let withMyError = possibleData.mapError { MyError.error($0) }
    

    Declaration

    Swift

    public func mapError<E>(_ transform: (Error) -> E) -> DownloadResponse where E : Error

    Parameters

    transform

    A closure that takes the error of the instance.

    Return Value

    A DownloadResponse instance containing the result of the transform.

  • Evaluates the specified closure when the DownloadResponse is a failure, passing the unwrapped error as a parameter.

    Use the flatMapError function with a closure that may throw an error. For example:

    let possibleData: DownloadResponse<Data> = ...
    let possibleObject = possibleData.flatMapError {
        try someFailableFunction(taking: $0)
    }
    

    Declaration

    Swift

    public func flatMapError<E>(_ transform: (Error) throws -> E) -> DownloadResponse where E : Error

    Parameters

    transform

    A throwing closure that takes the error of the instance.

    Return Value

    A DownloadResponse instance containing the result of the transform.

  • The task metrics containing the request / response statistics.

    Declaration

    Swift

    public var metrics: URLSessionTaskMetrics? { get }