I'm trying to send my API multiple photos iterating an image array. I want to send each image in a different thread but when the fist thread finish it doesn't wait for the rest of the threads and only one picture is sended.
This is my code:
// 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)
}
}
I've recently started programming in Swift and I don't know how to synchronize threads. Could you help me, please?
The code to do my requests is:
// 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")
}
}
}
The first image is uploaded correctly (response returns status code=200) but next images don't
This is the code I've modified following your advice:
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()