我正在使用阿尔莫菲尔为我的所有要求和工作良好。我需要知道如何概括所有请求,以便在一个地方处理所有错误。

func updateSettingValue(group : String , value: String , callback: @escaping (SettingsResponseModel) -> Void, errorCallback: @escaping (Error) ->Void)
{
    let url = BASE_URL_PROD + API_SETTINGS
    let settingsParams : Parameters = ["Setting" : group , "Tag" : value]

    Alamofire.request(url, method: .put, parameters: settingsParams,  headers: getHeader()).responseObject {
        (response: DataResponse< SettingsResponseModel>) in
        switch response.result {
        case .success:
            print("response \(response)")
            DispatchQueue.main.async {
                callback(response.result.value!)
            }
            break
        case .failure(let error):
            print(error)
            errorCallback(error)
        }
    }
}



func releaseKeys(mKey: String ,callback:  @escaping (ReleaseKeyModel) -> Void
    , errorCallback: @escaping (Error) -> Void){

    let url = BASE_URL_PROD + API_RELEASE_KEY

    let params: Parameters = ["mKey" : mKey]
    Alamofire.request(url, method: .delete, parameters: params, encoding: URLEncoding.default, headers: getHeader()).responseObject{
        (response : DataResponse< ReleaseKeyModel >) in
        print("releaseKey: \(response) ")

        switch response.result {
        case .success:
            DispatchQueue.main.async {
                callback(response.result.value!)
            }
            break
        case .failure(let error):
            print(error)
            errorCallback(error)
        }
    }
}

我如何将其推广到在DataResponse中获取映射模型类的参数,以便不必为所有方法单独处理成功和失败情况。

最佳答案

您可以将使用服务器的工作分为两类:
1)类RestClient

import Foundation

typealias responseBlock = (_ swiftObj: Any?, _ error: Error?) -> Void

class RestClient: NSObject {

    static let shared = RestClient()
    private var http = HttpService()

    func updateSettingValue(group: String, value: String, resp: @escaping responseBlock) {

        let url = BASE_URL_PROD + API_SETTINGS

        let params = ["Setting": group, "Tag": value]

        http.reque(url, method: .put, parameters: params, headers: getHeader(), resp: { (value, error) in

            if let err = error {
                return resp(nil, err)
            }

            guard let data = value else {
                return resp(nil, error)
            }

            //your method for parse data
            self.parseData(respData: data,
                           modelCls: SettingsResponseModel.self,
                           response: resp)
        })
    }

    func releaseKeys(mKey: String, resp: @escaping responseBlock) {

        let url = BASE_URL_PROD + API_RELEASE_KEY

        let params = ["mKey": mKey]

        http.reque(url, method: .delete, parameters: params, encoding: URLEncoding.default, headers: getHeader(), resp: { (value, error) in

            if let err = error {
                return resp(nil, err)
            }

            guard let data = value else {
                return resp(nil, error)
            }

            //your method for parse data
            self.parseData(respData: data,
                           modelCls: ReleaseKeyModel.self,
                           response: resp)
        })
    }

}

2)类HttpService
class HttpService {

    func reque(_ url: URLConvertible,
               method: HTTPMethod = .get,
               parameters: Parameters? = nil,
               encoding: ParameterEncoding = URLEncoding.default,
               headers: HTTPHeaders? = nil,
               queue: QueueQos = .defaultQos,
               resp: @escaping responseBlock) {

        Alamofire.request(url,
                          method: method,
                          parameters: parameters,
                          encoding: encoding,
                          headers: headers
            ).responseObject (queue: queue) { (response) in

                switch response.result {
                case .success:

                    if let jsonResp = response.result.value {

                        //You can also check out some error messages at this place.

                        resp(jsonResp, nil)
                    }

                case .failure(let error):
                    resp(nil, error)
                }
        }
    }

}

关于ios - Alamofire概括要求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52673526/

10-09 18:06