返回

Alamofire 源码学习:工具扩展相关

IOS

1. 工具扩展相关

在 Alamofire 中,对请求相关类型都通过扩展的方式添加了许多方法,这些方法有 Alamofire 内部业务相关,也有一些辅助工具功能的作用,看清楚后有助于我们对 Alamofire 更深层次的理解。

1.1 Request扩展

Request 扩展中主要是一些辅助方法,如:

/// Cancel the request.
public func cancel()

这个方法可以取消请求,在某些情况下,我们可能需要取消请求,比如在页面跳转的时候,为了防止上个页面的请求对当前页面造成影响,我们可以使用这个方法来取消请求。

/// Resume the request.
public func resume()

这个方法可以恢复请求,在某些情况下,我们可能需要暂停请求,比如在用户正在输入的时候,我们可以暂停请求,等到用户输入完成后再恢复请求。

/// Suspend the request.
public func suspend()

这个方法可以暂停请求,在某些情况下,我们可能需要暂停请求,比如在用户正在输入的时候,我们可以暂停请求,等到用户输入完成后再恢复请求。

1.2 DataRequest扩展

DataRequest 扩展中主要是一些数据处理的方法,如:

/// Validate the response, if response serialization succeeds.
///
/// - parameter validation: The closure that validates the response.
///
/// - returns: A new `Request` instance.
public func validate<S: Serializer>(_ validation: @escaping (DataResponse<S>) -> Request.ValidationResult) -> Self

这个方法可以对响应进行验证,如果响应序列化成功,则调用验证闭包,如果验证闭包返回 true,则请求成功,否则请求失败。

/// Perform any validation that's needed prior to serialization.
///
/// - parameter validation: The closure that validates the response.
///
/// - returns: A new `Request` instance.
public func validate<S: Serializer>(_ validation: @escaping (URLRequest, HTTPURLResponse, Data?) -> Request.ValidationResult) -> Self

这个方法可以对响应进行验证,在序列化之前进行验证,如果验证闭包返回 true,则请求成功,否则请求失败。

/// Serialize the response using the given serializer and return a `Response` instance containing the result.
///
/// - parameter queue:              The queue on which the completion handler is dispatched. `.main` by default.
/// - parameter serializer:         The serializer to use.
/// - parameter completionHandler: A closure to be executed once the serialization is complete.
///
/// - returns: A new `DataRequest` instance.
public func response<S: Serializer>(queue: DispatchQueue = .main, serializer: S, completionHandler: @escaping (DataResponse<S>) -> Void) -> Self

这个方法可以将响应序列化为指定类型的对象,并调用完成处理程序,如果序列化成功,则完成处理程序会收到一个成功响应,否则完成处理程序会收到一个失败响应。

1.3 DownloadRequest扩展

DownloadRequest 扩展中主要是一些下载相关的方法,如:

/// Resume the download.
public func resume()

这个方法可以恢复下载,在某些情况下,我们可能需要暂停下载,比如在用户正在输入的时候,我们可以暂停下载,等到用户输入完成后再恢复下载。

/// Suspend the download.
public func suspend()

这个方法可以暂停下载,在某些情况下,我们可能需要暂停下载,比如在用户正在输入的时候,我们可以暂停下载,等到用户输入完成后再恢复下载。

/// Cancel the download.
public func cancel()

这个方法可以取消下载,在某些情况下,我们可能需要取消下载,比如在页面跳转的时候,为了防止上个页面的下载对当前页面造成影响,我们可以使用这个方法来取消下载。

2. 实例

2.1 取消请求

// Create a request.
let request = Alamofire.request("https://example.com/api/v1/users")

// Cancel the request.
request.cancel()

2.2 恢复请求

// Create a request.
let request = Alamofire.request("https://example.com/api/v1/users")

// Suspend the request.
request.suspend()

// Resume the request.
request.resume()

2.3 暂停请求

// Create a request.
let request = Alamofire.request("https://example.com/api/v1/users")

// Suspend the request.
request.suspend()

2.4 对响应进行验证

// Create a request.
let request = Alamofire.request("https://example.com/api/v1/users")

// Validate the response.
request.validate().responseJSON { (response) in
    // Handle the response.
}

2.5 将响应序列化为指定类型的对象

// Create a request.
let request = Alamofire.request("https://example.com/api/v1/users")

// Serialize the response.
request.responseJSON { (response) in
    // Handle the response.
}

2.6 恢复下载

// Create a download request.
let downloadRequest = Alamofire.download("https://example.com/file.zip")

// Suspend the download.
downloadRequest.suspend()

// Resume the download.
downloadRequest.resume()

2.7 暂停下载

// Create a download request.
let downloadRequest = Alamofire.download("https://example.com/file.zip")

// Suspend the download.
downloadRequest.suspend()

2.8 取消下载

// Create a download request.
let downloadRequest = Alamofire.download("https://example.com/file.zip")

// Cancel the download.
downloadRequest.cancel()