首页 > 解决方案 > Alamofire 后台获取不处理更进一步的请求

问题描述

我想使用 执行后台提取performFetchWithCompletionHandler,显然所有以前的解决方案都对我不起作用。

奇怪的是我的第一个请求是好的,但下一个不是。

我尝试使用单例 SessionManager,我还尝试将所有 Alamofire 请求出列到后台线程,并更改了URLSessionManagerwith URLSessionConfiguration.background(withIdentifier: "com.test.background")

 group.enter()
 api.getClimateStateData(carId: carId, completion: { [weak self] response in
       guard let strongSelf = self else { return }
       climateModel = strongSelf.sortDataFromError(data: response)
       group.leave()
})
func getClimateStateData(carId: Int, completion: @escaping (ServiceResponse<ClimateStateModel>) -> Void) {
        let header: HTTPHeaders = ["User-Agent": UserAgentHelper.fullUserAgentString]
        request("/api/1/vehicles/\(carId)/data_request/climate_state", method: .get, parameters: nil, encoding: nil, headers: header).responseData { [weak self] response in
            guard let strongSelf = self else { return }
            completion(strongSelf.completionResponse(response))
     }
}
    @discardableResult private func request(
        _ path: String,
        method: HTTPMethod,
        parameters: Parameters? = nil,
        encoding: ParameterEncoding? = nil,
        headers: HTTPHeaders? = nil)
        -> DataRequest {
            let userEncoding = encoding ?? self.defaultEncoding
            print("Asking: " + path)
            return Service.sessionManager2.request("\(API)\(path)", method: method, parameters: parameters, encoding: userEncoding, headers: headers).validate()
}

我希望满足这些要求。

谢谢你的时间!

标签: iosswiftbackgroundalamofire

解决方案


我的错,这是由于self= nil。

由于某些原因,self被 deinit。


推荐阅读