我想用performFetchWithCompletionHandler
执行后台抓取,显然所有以前的解决方案都不适合我。
奇怪的是我的第一个请求是可以的,但接下来的请求不是。
我尝试使用单个SessionManager,也尝试将所有Alamofire请求出队到后台线程,并用URLSessionManager
更改了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()
}
我希望这些请求得到满足。
感谢您的时间!
答案 0 :(得分:0)
我不好,这是由于self
= nil。
由于某些原因,self
被取消了初始化。