【iOS Swift 开发】Alamofire 同步回调(基于 PromiseKit&AwaitKit 实现)

使用 PromiseKit扩展 Alamofire,通过 AwaitKit 同步获取回调, 使 Alamofire 支持 promise/await/async 调用

Related swift packages

Required Alamofire&AwaitKit

Package.swift

 dependencies: [
            .package(url: "https://github.com/Alamofire/Alamofire.git", from: "4.9.1"),
            .package(url: "https://github.com/yannickl/AwaitKit.git", from:"5.2.0")
        ],
 targets: [
            .target(
                    name: "<Your-Package-Name>",
                    dependencies: [
                        "Alamofire",
                        "AwaitKit"
                    ])
        ],

Source code

//
// Created by Axios on 2020/1/14.
//

import Foundation
import Alamofire
import PromiseKit

extension Alamofire.DataRequest {
    public func response(queue: DispatchQueue? = nil) -> Promise<DefaultDataResponse> {
        let RequestQueue: DispatchQueue = DispatchQueue(label: "RequestQueue.Alamofire.DataRequest.response", attributes: .init(rawValue: 0))
        return Promise { seal in
            response(queue: RequestQueue) { response in
                seal.fulfill(response)
            }
        }
    }

    public func responseString() -> Promise<DataResponse<String>> {
        let RequestQueue: DispatchQueue = DispatchQueue(label: "RequestQueue.Alamofire.DataRequest.responseString", attributes: .init(rawValue: 0))
        return Promise { seal in
            responseString(queue: RequestQueue) { response in
                seal.fulfill(response)
            }
        }
    }

    public func responseData() -> Promise<DataResponse<Data>> {
        let RequestQueue: DispatchQueue = DispatchQueue(label: "RequestQueue.Alamofire.DataRequest.responseData", attributes: .init(rawValue: 0))
        return Promise { seal in
            responseData(queue: RequestQueue) { response in
                seal.fulfill(response)
            }
        }
    }

    public func responseJSON(options: JSONSerialization.ReadingOptions = .allowFragments) -> Promise<DataResponse<Any>> {
        let RequestQueue: DispatchQueue = DispatchQueue(label: "RequestQueue.Alamofire.DataRequest.responseJSON", attributes: .init(rawValue: 0))
        return Promise { seal in
            responseJSON(queue: RequestQueue, options: options) { response in
                seal.fulfill(response)
            }
        }
    }

    public func responsePropertyList(
            queue: DispatchQueue? = nil,
            options: PropertyListSerialization.ReadOptions = PropertyListSerialization.ReadOptions()
    ) -> Promise<DataResponse<Any>> {
        let RequestQueue: DispatchQueue = DispatchQueue(label: "RequestQueue.Alamofire.DataRequest.responsePropertyList", attributes: .init(rawValue: 0))
        return Promise { seal in
            responsePropertyList(queue: RequestQueue, options: options) { response in
                seal.fulfill(response)
            }
        }
    }
}

extension Alamofire.DownloadRequest {
    public func response(_: PMKNamespacer, queue: DispatchQueue? = nil) -> Promise<DefaultDownloadResponse> {
        let RequestQueue: DispatchQueue = DispatchQueue(label: "RequestQueue.Alamofire.DownloadRequest.response", attributes: .init(rawValue: 0))
        return Promise { seal in
            response(queue: RequestQueue) { response in
                seal.fulfill(response)
            }
        }
    }

    public func responseData(queue: DispatchQueue? = nil) -> Promise<DownloadResponse<Data>> {
        let RequestQueue: DispatchQueue = DispatchQueue(label: "RequestQueue.Alamofire.DownloadRequest.responseData", attributes: .init(rawValue: 0))
        return Promise { seal in
            responseData(queue: RequestQueue) { response in
                seal.fulfill(response)
            }
        }
    }
}

Usage example

let promise = Alamofire.request("https://httpbin.org/get", method: HTTPMethod.get).response()
let res: DefaultDataResponse = try! await(promise)
XCTAssertNotNil(res)
let result: String = String(data: res.data!, encoding: .utf8) ?? ""
XCTAssertFalse(result.isEmpty)
添加新评论