gpt4 book ai didi

ios - 并行http请求

转载 作者:行者123 更新时间:2023-12-01 16:21:16 24 4
gpt4 key购买 nike

我需要将image 发送到服务器并等待服务器响应。图片很大,需要很长时间,所以我想将其并行化。我尝试了很多方法,但出现错误:

2019-12-20 15:01:49.148156+0100 Catch Photo[644:138111] Task <81527D6C-279F-4644-9F5A-DE00665BCBF2>.<18> finished with error [-1001] Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={_kCFStreamErrorCodeKey=-2102, NSUnderlyingError=0x282184240 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalUploadTask <81527D6C-279F-4644-9F5A-DE00665BCBF2>.<18>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalUploadTask <81527D6C-279F-4644-9F5A-DE00665BCBF2>.<18>" ), NSLocalizedDescription=The request timed out.

exteriorQueinAsinc.async {
let queue = OperationQueue()
for i in 0..<images.count
{
queue.addOperation {
self.sendImageToServer(image: images[i], imageName: imageNames[i])
}
}
queue.waitUntilAllOperationsAreFinished()
}

发送图片函数:

 func SendImageToServer(image:UIImage,imageName:String){
let userId = AppSettings.user.id
let token = AppSettings.token

let url = "http://intaiapi.catch-system.com/vehicle_image"
guard let imageData = image.jpegData(compressionQuality: 1.0) else { return }

let parametar : [String:String] = [
"apiUrl":apiUrl,
"userId":userId!,
"userToken":token,
]

let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 1200
manager.upload(multipartFormData: { multipleData in
for (key, value) in parametar {
multipleData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
multipleData.append(
imageData, withName: "image", fileName: "image.jpeg", mimeType: "image/jpeg")}, to: url) {
(result) in
switch result {

case .success(let upload, _, _):
upload.responseJSON { response in
if let image = UIImage(data: response.data!) {
handler(.success(image))
} else {
NotificationCenter.default.post(name: .ClearImageErrorNotification, object: nil)
handler(.failure(.NotValidStatusCode(response.debugDescription)))
}
}
case .failure(let encodingError):
NotificationCenter.default.post(name: .ClearImageErrorNotification, object: nil)
handler(.failure(.NotValidStatusCode(encodingError.localizedDescription)))
print(encodingError)
}
}
}

我尝试过的方法之一就是这个。我使用 Alamofire。

最佳答案

您可以增加等待请求完成的超时时间。设置

manager.session.configuration.timeoutIntervalForResource = 1200

或任何其他接近

的值
manager.session.configuration.timeoutIntervalForRequest = 1200

关于ios - 并行http请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59426759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com