2016-10-24 38 views
0

我有NSMutableDictionary我想要下载的数组列表。我正在使用cellForRowAtIndexPath来下载它们中的每一个。但是,当cellForRowAtIndexPath运行时,并行下载的所有zip文件都会导致应用程序挂起,UI冻结,CPU使用率通过屋顶。Alamofire:如何在UITableView中顺序下载zip文件

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 = Utility().getContentsDirectory().stringByAppendingString("/\(fileName).zip") 
    Alamofire.download(.GET, source, destination: destination) 
    .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in 
     println(totalBytesRead) // update progressView and labelView 
    } 
    .response { request, response, _, error in 
     println(response) 
    } 
} 

他们可以一个接一个顺序下载吗?谢谢。

回答

0

您面临的问题是,只要表格请求相关单元格就会进行下载调用,因为Alamofire会以异步方式执行所有操作(如果不是,您会等待文件下载之前你甚至会看到这些单元格)。

你想要做的是实现一个堆栈,它将排队你的请求,并且在前一个请求完成后立即弹出下一个请求。

+0

现在,我通过删除NSmutableDictionary得到了结果。 在cellForRowAtIndexPath中,我检查了indexPath.row。 if indexPath.row == 0 {self.updateBookList(row,...)} 在updateBookList中,我像“self.bookArray.removeObject(bookData)”一样被删除。感谢您的回复。 –

+1

对不起,解决您在评论中遇到的问题有点难。就我个人而言,我认为你应该重新考虑下载“cellForRowAtIndexPath”,因为这也意味着每次新单元出现时(即滚动时)都会触发下载。 – PeterB