Too many open files while download photos with Alamofire.download

Hi, i have a problem, while download photos from my server get this error.


Error dnssd_clientstub deliver_request: socketpair failed 24 (Too many open files)

__NSCFLocalDownloadFile: error 24 opening for ioChannel, file: /private/var/mobile/Containers/Data/Application/6C28AF76-1BFA-4324-935A-E3655D273986/tmp/CFNetworkDownload_6VkNHA.tmp


There are 5000 photos and only download 300. I think alamofire download all photos simultaneus and the iphone memory crash.


This is my source code and my platform is, xcode 8, swift 3.0.1 and iphone 6 64gb with the latest iOS,


Thanks.


func SyncFotos() {

let parametro: Parameters = [

"empresa": "lita8x",

"inventario": "1"

]

self.lblSync2.text = "Sincronizando Fotos"

Alamofire.request("http://litani2.dyndns.org:8080/litani-web/webservice/articulosa.php", parameters: parametro, encoding: URLEncoding.default)

.responseJSON { response in

if let result = response.result.value {

if((result) != nil) {

let swiftyJsonVar = JSON(result)

if let resData = swiftyJsonVar["productos"].arrayObject {

let arrRes = resData as! [[String:AnyObject]]

let xcuenta = arrRes.count

var xconteo = 0

DispatchQueue.global(qos: .background).async {

for articulo in arrRes {

let xcodigo = articulo["codigo"] as! String

let nombre = xcodigo+".jpg"

// Eliminamos la foto primero

let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

let filePath = documentsURL.appendingPathComponent(nombre).path

if FileManager.default.fileExists(atPath: filePath) {

// try! FileManager.default.removeItem(atPath: filePath)

} else {

let url = "http://litani2.dyndns.org:8080/litani-web/imagenes/"+nombre

let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)

Alamofire.download(url, to: destination).response { response in

print("DEBUG: Request \(nombre)")

}

print(url)

}

xconteo=xconteo+1

DispatchQueue.main.async {

self.lblSync2.text = "Sincronizando Fotos \(xconteo) de \(xcuenta)"

if (xconteo == xcuenta) {

self.lblSync2.text = "Fotos Sincronizadas"

}

}

}

} // Dispatch

} // if let resData

} // if((result) != nil)

} // if let result

self.lblSync2.text = "Sincronizar Fotos Culminado"

}

}

Replies

Please following the debugging advice from this post and let us know what you see.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"