关键信息:
Alamofire在拨打某些电话后停止拨打电话。我没有收到回叫,一旦它停止工作,我就看不到Charles
中的任何网络呼叫。
我使用管理器进行调用,管理器又调用一个帮助客户端类,它向Alamofire请求一个请求对象,我将其存储在客户端的数组中。您可以在下面看到我的课程的部分代码。
// This is in BaseClient
/* GET Request
default response with mappable data and NSerror
*/
@discardableResult
class func getRequest<T: Mappable>(_ apiRoute: String, params: [String: Any], credentialsRequired: Bool = false, completion: @escaping (T?, Error?) -> Void) -> DataRequest {
let headers = additionalHeaders(credentialsRequired)
let request = Alamofire.request(getURL(for: apiRoute), method: .get, parameters: params, headers: headers)
.responseObject { (response: DataResponse<DefaultResponse<T>>) -> Void in
logResponse(response.result.value)
if let error = self.checkForError(response, endPoint: apiRoute) {
completion(nil, error)
} else {
completion(response.result.value?.data, nil)
}
}
logCurl(request)
return request
}
这是帮助客户端类
import ObjectMapper
import Alamofire
class PackageClient: BaseClient {
fileprivate struct PackagesEndPoint {
static let inspire = "/api/v2/something"
}
fileprivate static var currentRequests = [Request]()
class func cancelAllFetchRequests() {
for item in currentRequests {
item.cancel()
}
currentRequests = [Request]()
}
class func fetchInspire(_ completion: @escaping (_ response: InspireResponse?, _ error: Error?) -> Void) {
PackageClient.cancelAllFetchRequests()
var params = [String: Any]()
params["product"] = "flight,event,package"
let request = getRequest(PackagesEndPoint.inspire,
params: params,
credentialsRequired: false){ (response: InspireResponse?, error: Error?) in
completion(response, error)
}
currentRequests.append(request)
}
}
这是在PackageManager中:
PackageClient.fetchInspire { [weak self] (response, error) in
if error == nil {
self?.inspireResponse = response
}
self?.finishLoading(error: error)
}
问题是一旦Alamofire停止工作,那么像下面这样的简单电话也不会进行任何网络通话......我在查尔斯上看不到任何内容,显然没有回复任何回调。
Alamofire.request(URL(string: urlString), method: .get, parameters: parameters, encoding: JSONEncoding.default)
.validate { request, response, data in
// Custom evaluation closure now includes data (allows you to parse data to dig out error messages if necessary)
return .success
}
.responseJSON { response in
debugPrint(response)
}
感谢任何帮助。我花了两天时间来调试它。 :(
答案 0 :(得分:1)
这也发生在我身上。我在我的代码中使用objc_sync_enter(self)
调用导致死锁。