BatchRequest

open class BatchRequest : HTTPRequestProtocol, RequestDelegate, @unchecked Sendable

批量请求类

  • 批量请求完成句柄

    Declaration

    Swift

    public typealias Completion = @MainActor @Sendable (BatchRequest) -> Void

Accessor

  • 当前请求数组

    Declaration

    Swift

    open private(set) var requestArray: [HTTPRequest] { get }
  • 事件代理

    Declaration

    Swift

    open weak var delegate: BatchRequestDelegate?
  • 成功完成回调

    Declaration

    Swift

    open var successCompletionBlock: Completion?
  • 失败完成回调

    Declaration

    Swift

    open var failureCompletionBlock: Completion?
  • 自定义取消回调句柄,不一定主线程调用

    Declaration

    Swift

    open var requestCancelledBlock: (@Sendable (BatchRequest) -> Void)?
  • tag

    请求标签,默认0

    Declaration

    Swift

    open var tag: Int
  • 自定义请求配件数组

    Declaration

    Swift

    open var requestAccessories: [RequestAccessoryProtocol]?
  • 第一个导致批量请求失败的请求

    Declaration

    Swift

    open var failedRequest: HTTPRequest? { get }
  • 是否自动显示错误信息

    Declaration

    Swift

    open var autoShowError: Bool { get set }
  • 当前网络错误

    Declaration

    Swift

    open var error: Error? { get }
  • 已失败请求数组

    Declaration

    Swift

    open private(set) var failedRequestArray: [HTTPRequest] { get }
  • 请求是否已取消

    Declaration

    Swift

    open private(set) var isCancelled: Bool { get }
  • 某个请求失败时,是否立即停止批量请求,默认true

    Declaration

    Swift

    open var stoppedOnFailure: Bool
  • 是否所有响应数据都来自本地缓存

    Declaration

    Swift

    open var isDataFromCache: Bool { get }

Lifecycle

Public

  • 添加单个请求,start之前调用

    Declaration

    Swift

    @discardableResult
    open func addRequest(_ request: HTTPRequest) -> Self
  • 开始请求,仅能调用一次

    Declaration

    Swift

    @discardableResult
    open func start() -> Self
  • 取消请求

    Declaration

    Swift

    open func cancel()
  • 开始请求并指定成功、失败句柄

    Declaration

    Swift

    @discardableResult
    open func start(success: Completion?, failure: Completion?) -> Self
  • 开始请求并指定完成句柄

    Declaration

    Swift

    @discardableResult
    open func start(completion: Completion?) -> Self
  • 请求取消句柄,不一定主线程调用

    Declaration

    Swift

    @discardableResult
    open func requestCancelledBlock(_ block: (@Sendable (BatchRequest) -> Void)?) -> Self
  • 自定义响应完成句柄

    Declaration

    Swift

    @discardableResult
    open func response(_ completion: Completion?) -> Self
  • 自定义响应成功句柄

    Declaration

    Swift

    @discardableResult
    open func responseSuccess(_ block: Completion?) -> Self
  • 自定义响应失败句柄

    Declaration

    Swift

    @discardableResult
    open func responseFailure(_ block: Completion?) -> Self
  • 快捷设置响应失败句柄

    Declaration

    Swift

    @discardableResult
    open func responseError(_ block: (@MainActor @Sendable (Error) -> Void)?) -> Self
  • 清理完成句柄

    Declaration

    Swift

    open func clearCompletionBlock()
  • 显示网络错误,默认显示Toast提示

    Declaration

    Swift

    open func showError()
  • 添加请求配件

    Declaration

    Swift

    @discardableResult
    open func addAccessory(_ accessory: RequestAccessoryProtocol) -> Self
  • 请求完成回调

    Declaration

    Swift

    @MainActor
    open func requestFinished(_ request: HTTPRequest)
  • 请求失败回调

    Declaration

    Swift

    @MainActor
    open func requestFailed(_ request: HTTPRequest)

Concurrency+BatchRequest

  • response() Asynchronous

    异步获取完成响应,注意非Task取消也会触发(Continuation流程)

    Declaration

    Swift

    public func response() async -> BatchRequest
  • responseSuccess() Asynchronous

    异步获取成功响应,注意非Task取消也会触发(Continuation流程)

    Declaration

    Swift

    public func responseSuccess() async throws -> BatchRequest