我试图发送我的API迭代图像数组的多张照片。 我想发送不同的线程中的每个图像,但是当第一个线程 完成它不等待其余的线程和发送只有一个图片 。Swift 3:执行多个线程并等待敲定
这是我的代码:
// create the concurrent queue
let asyncQueue = DispatchQueue(label: "asyncQueue", attributes: .concurrent)
// perform the task asynchronously
for image in self.arrayImages{
asyncQueue.async() {
let strBase64:String = image.base64EncodedString(options: .lineLength64Characters)
self.sendImage(image: strBase64)
}
}
我最近在斯威夫特开始编程,我不知道如何同步线程。请问你能帮帮我吗?
代码来执行我的要求是:
// Función para mandar a la API la petición de añadir una imagen a la idea
func sendImage(image:String){
let data = NSMutableDictionary()
let id:Int = (idea?.id)!
let pasoAct = idea?.pasoActual
data["id_idea"] = id
data["id_paso"] = pasoAct
data["contenido"] = image
let jsonData = try! JSONSerialization.data(withJSONObject: data, options: JSONSerialization.WritingOptions(rawValue: 0))
let request = NSMutableURLRequest(url: NSURL(string:"http://192.168.2.101:3001/api/imagen") as! URL)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.httpBody = jsonData
URLSession.shared.dataTask(with: request as URLRequest, completionHandler: self.responseImage).resume()
}
// Función de control de la respuesta del servidor tras enviar una imagen
func responseImage(data: Data?, response: URLResponse?, error: Error?) {
if (error != nil) {
print("No se ha podido contactar con el servidor")
}
else{
// Connected, check response, GET status codes.
let statusCode = (response as! HTTPURLResponse).statusCode
switch statusCode{
case 200:
print("Imagen subida")
default:
print("Error en la petición al servidor")
}
}
}
第一幅图像正确上传(响应返回状态码= 200),但接下来的图像不
这是我的代码修改以下的建议:
func sendImage(image:String, completion: @escaping() -> Void) {
let data = NSMutableDictionary()
let id:Int = (idea?.id)!
let pasoAct = idea?.pasoActual
data["id_idea"] = id
data["id_paso"] = pasoAct
data["contenido"] = image
let jsonData = try! JSONSerialization.data(withJSONObject: data, options: JSONSerialization.WritingOptions(rawValue: 0))
let request = NSMutableURLRequest(url: NSURL(string:"http://192.168.2.101:3001/api/imagen") as! URL)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.httpBody = jsonData
URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
self.responseImage(data: data, response: response, error: error)
// When responseImage is complete, call the completion handler
completion()
}
}
// And the following code is called when I try to send the array of images
let group = DispatchGroup()
let asyncQueue = DispatchQueue(label: "asyncQueue", attributes: .concurrent)
for image in self.arrayImages {
asyncQueue.async {
group.enter()
let strBase64:String = image.base64EncodedString(options: .lineLength64Characters)
self.sendImage(image: strBase64) {
group.leave()
}
}
}
group.wait()
同意。以防所有的异步基本上执行相同的代码,我也建议这个 –
不适合我:( –