Thant Han Lin Thant Han Lin - 4 months ago 59
iOS Question

Alamofire: How to download zip files sequentially within UITableView

I have NSMutableDictionary Array lists that i want to download. I am using cellForRowAtIndexPath to download each of them. However when the cellForRowAtIndexPath runs, all the zip files download in parallel, which causes the app to hang, the UI to freeze, CPU use to go through the roof.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:updateAllCell = tableView.dequeueReusableCellWithIdentifier("updateAllCell") as! updateAllCell!
let row = self.bookArray.objectAtIndex(indexPath.row) as! NSMutableDictionary
self.updateBookList(row, progressView: cell.downloadProgressView, labelView: cell.lblDownloadPercent)

func updateBookList(bookData: NSMutableDictionary, progressView: UIProgressView, labelView: UILabel) {
let source = BookData.objectForKey("url") as! String, source, destination: destination)
.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
println(totalBytesRead) // update progressView and labelView
.response { request, response, _, error in

Can it download one by one sequentially? Thank.


The problem you are facing is that the download calls are being made as soon as the table requests the cell in question, since Alamofire does everything asynchronously (and if it didn't you would be waiting for the files to download before you would even see the cells).

What you want to do is implement a stack that will queue your requests and you pop the next request as soon as the previous one is finished.