Repository: Alamofire/Alamofire
File: Source/Request.swift (Swift)

Code before:
//  THE SOFTWARE.
//

import Foundation

/// Responsible for sending a request and receiving the response and associated data from the server, as well as
/// managing its underlying `URLSessionTask`.
public class Request {
    /// A closure executed once a request has successfully completed in order to determine where to move the temporary
    /// file written to during the download process. The closure takes two arguments: the temporary file URL and the URL
    /// response, and returns a single argument: the file URL where the temporary file should be moved.
    public typealias DownloadFileDestination = (URL, HTTPURLResponse) -> URL

    // MARK: Properties

    /// The delegate for the underlying task.
    public let delegate: TaskDelegate

    /// The underlying task.
    public var task: URLSessionTask { return delegate.task }

    /// The session belonging to the underlying task.
    public let session: URLSession

    /// The request sent or to be sent to the server.
    public var request: URLRequest? { return task.originalRequest }

    /// The response received from the server, if any.
    public var response: HTTPURLResponse? { return task.response as? HTTPURLResponse }

    /// The progress of the request lifecycle.
    public var progress: Progress { return delegate.progress }

    /// The resume data of the underlying download task if available after a failure.
    public var resumeData: Data? {
        var data: Data?

        if let delegate = delegate as? DownloadTaskDelegate {
            data = delegate.resumeData
        }

        return data
    }

    var startTime: CFAbsoluteTime?
    var endTime: CFAbsoluteTime?

    // MARK: Lifecycle

    init(session: URLSession, task: URLSessionTask) {
        self.session = session

Review comment (suggestion):
If we decide this is unnecessary, then it's really simple. We just need to remove the `taskDelegate` and the `taskDelegateLock` and revert this change while maintaining the `internal(set) var delegate` change.


Code after:
//  THE SOFTWARE.
//

import Foundation

/// Responsible for sending a request and receiving the response and associated data from the server, as well as
/// managing its underlying `URLSessionTask`.
public class Request {
    /// A closure executed once a request has successfully completed in order to determine where to move the temporary
    /// file written to during the download process. The closure takes two arguments: the temporary file URL and the URL
    /// response, and returns a single argument: the file URL where the temporary file should be moved.
    public typealias DownloadFileDestination = (URL, HTTPURLResponse) -> URL

    // MARK: Properties

    /// The delegate for the underlying task.
    public internal(set) var delegate: TaskDelegate {
        get {
            taskDelegateLock.lock() ; defer { taskDelegateLock.unlock() }
            return taskDelegate
        }
        set {
            taskDelegateLock.lock() ; defer { taskDelegateLock.unlock() }
            taskDelegate = newValue
        }
    }

    /// The underlying task.
    public var task: URLSessionTask { return delegate.task }

    /// The session belonging to the underlying task.
    public let session: URLSession

    /// The request sent or to be sent to the server.
    public var request: URLRequest? { return task.originalRequest }

    /// The response received from the server, if any.
    public var response: HTTPURLResponse? { return task.response as? HTTPURLResponse }

    /// The progress of the request lifecycle.
    public var progress: Progress { return delegate.progress }

    /// The resume data of the underlying download task if available after a failure.
    public var resumeData: Data? {
        var data: Data?

        if let delegate = delegate as? DownloadTaskDelegate {
            data = delegate.resumeData
        }

        return data