【问题标题】:Alamofire: How to download zip files sequentially within UITableViewAlamofire:如何在 UITableView 中按顺序下载 zip 文件
【发布时间】:2016-10-24 10:06:03
【问题描述】:

我有要下载的NSMutableDictionary 数组列表。我正在使用cellForRowAtIndexPath 下载它们中的每一个。但是,当cellForRowAtIndexPath 运行时,所有的 zip 文件并行下载,这导致应用程序挂起、UI 冻结、CPU 使用率飙升。

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)
     }
}

他们可以依次下载吗?谢谢。

【问题讨论】:

    标签: ios swift uitableview alamofire


    【解决方案1】:

    您面临的问题是,一旦表格请求有问题的单元格,就会进行下载调用,因为 Alamofire 会异步执行所有操作(如果不是,您将等待文件在您之前下载甚至会看到细胞)。

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

    【讨论】:

    • 现在,我通过删除 NSmutableDictionary 得到了结果。在 cellForRowAtIndexPath 中,我检查了 indexPath.row。 if indexPath.row == 0 { self.updateBookList(row,...) } 在 updateBookList 中,我删除了“self.bookArray.removeObject(bookData)”。感谢您的回复。
    • 抱歉,从您的评论中很难解决您面临的问题。就个人而言,我认为您应该重新考虑在“cellForRowAtIndexPath”上下载,因为这也意味着每次出现新单元格时(即滚动时)都会触发下载。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多