public func FE_Request(with identifier: String,
urlString: String,
method: HTTPMethod = .get,
parameters: Parameters?,
success: @escaping ResultHandler,
failure: @escaping ResultHandler)
-> URLSessionTask? {
if let task = self.caches[identifier] {
failure(["message":"This request is exist, Please wait a moment","code":NSURLErrorCancelled])
return task
}
print("urlString=" + urlString)
if let p = parameters {
FELogDebug("parameters=\(p)")
}
let mesage = NSLocalizedString("translate.1x", comment: "请求链接不能为空!")
assert(!urlString.isEmpty, mesage)
var headers: HTTPHeaders = [
"Accept": "application/json,image/*",
"Cache-Control": "no-cache",
"Content-type": "application/json"
]
headers["User-Agent"] = self.userAgent;
headers["X-DEVICE-ID"] = UIDevice.IDFV;
if let language = feUD.string(forKey: FELanguageKey), !language.isEmpty {
var acceptLanguage = language
if let regionCode = Locale.current.regionCode {
acceptLanguage = "\(language)-\(regionCode)"
}
headers["Accept-Language"] = acceptLanguage
}
if let loginInfo = FELauncher.sharedInstance.getLoginInfo() {
if loginInfo.customerType == .Enterprise {
headers["X-KA-SESSION-ID"] = loginInfo.sessionID
}else if loginInfo.customerType == .Personal {
headers["X-FLE-SESSION-ID"] = loginInfo.sessionID
}
}
let dataRequest = Alamofire.request(urlString,
method: method,
parameters: parameters,
encoding: JSONEncoding.`default`,
headers: headers)
.responseJSON { [weak self](jsonResponse) in
FELogDebug("[REQUEST:]\(String(describing: jsonResponse.request)),[RESPONSE:]\(jsonResponse)")
if let statusCode = jsonResponse.response?.statusCode {
if let json = jsonResponse.value {
if statusCode >= 200 && statusCode < 300 {
//successed
success(json)
}else {
//failure
failure(json)
}
}else{
failure(["message":"invalid json"])
}
}else{
//没有获取到状态码,发生http错误。查看http错误码
if let responseError = jsonResponse.error {
let error = responseError as NSError
print(error)
if error.code != NSURLErrorCancelled {
failure(["message":error.localizedDescription,"code":error.code])
}
}else {
//error
failure(["message":"Request Error!"])
}
}
self?.caches.removeValue(forKey: identifier)
}
if let task = dataRequest.task {
self.caches[identifier] = task
}
return dataRequest.task
}
Alamofire Environment
Версия Alamofire: 4.7.3
Версия Xcode: 11.4
Версия Swift: 4
Cra sh Изображение
Извините за плохой английский sh!
Это старая проблема этой cra sh , Лишь изредка в cra sh log! И я часто получаю другую cra sh log «специализированный словарь. .getter »тем же методом. Будет ли это проблемой потока? Я обеспокоен. Спасибо, что позаботились об этом.