gpt4 book ai didi

ios - swift iOS - NSURLSession 无法正确下载多个文件

转载 作者:行者123 更新时间:2023-11-30 14:02:39 24 4
gpt4 key购买 nike

我的应用程序同时下载多个文件。我的场景如下:

  1. 用户点击下载按钮。
  2. 初始化一个Download UITableViewCell,它实现NSURLSessionDelegate来下载指定文件。
  3. 下载已开始。下载单元显示使用 NSURLSession 提供的委托(delegate)下载文件的进度。
  4. 文件保存在本地文档文件夹中。
  5. 用户单击按钮清除所有已完成的下载。此按钮清除下载单元使用的数组“downloadQueue”。该按钮还调用tableView.reloadData来更新tableView

问题是,当用户发起多次下载时,不会出现问题。所有文件同时正确下载。但是,当用户单击步骤 5 中描述的按钮,然后尝试下载另一个文件时,下载进度立即显示 100% 完成。并且本地没有保存任何文件。这可能是由于立即调用 didFinishDownloadingToURL 所致。

这是我的 DownloadCell.swift 的完整代码

class DownloadCell: UITableViewCell, NSURLSessionDelegate {


@IBOutlet weak var lblSongName: UILabel!
@IBOutlet weak var progressBar: UIProgressView!
@IBOutlet weak var progressCount: UILabel!

var song: Song!

var task: NSURLSessionTask!

var percentageWritten: Float = 0.0
var taskTotalBytesWritten = 0
var taskTotalBytesExpectedToWrite = 0

lazy var session: NSURLSession = {
let config = NSURLSessionConfiguration.ephemeralSessionConfiguration()
config.allowsCellularAccess = false
let session = NSURLSession(configuration: config, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
return session
}()


override func awakeFromNib() {
super.awakeFromNib()
// Initialization code

}

func startDownload() {

lblSongName.text = song.songName

if self.task != nil {
return
}
let url = NSURL(string: song.songDownloadLink)!
let req = NSMutableURLRequest(URL: url)
let task = self.session.downloadTaskWithRequest(req)
self.task = task
task.resume()

}

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten writ: Int64, totalBytesExpectedToWrite exp: Int64) {
taskTotalBytesWritten = Int(writ)
taskTotalBytesExpectedToWrite = Int(exp)
percentageWritten = Float(taskTotalBytesWritten) / Float(taskTotalBytesExpectedToWrite)
progressBar.progress = percentageWritten
progressCount.text = String(Int(percentageWritten*100)) + "%"

print("(\(taskTotalBytesWritten) / \(taskTotalBytesExpectedToWrite))")
}

func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
if error != nil {
print("Completed with error: \(error)")
}
}

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {

do {
let documentsDirectoryURL = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as NSURL!
progressCount.text = "Done!"
progressBar.progress = 100.0

try NSFileManager().moveItemAtURL(location, toURL: documentsDirectoryURL.URLByAppendingPathComponent(song.songName + ".mp3"))

//Handling Badge Values
let tabBar = UIApplication.sharedApplication().keyWindow?.rootViewController as! UITabBarController
let downloadVC = tabBar.viewControllers?[1] as! DownloadViewController
let badgeValue = Int(downloadVC.tabBarItem.badgeValue!)! - 1
downloadVC.tabBarItem.badgeValue = String(badgeValue)

if badgeValue == 0 {
downloadVC.tabBarItem.badgeValue = nil
}

} catch {
print("Error occurred during saving.")
progressCount.text = String(error)

}

}

}

最佳答案

也许你应该实现uitableviewcell的prepareForReuse函数?如果这些单元被重用,它们仍然可能有任务,因此 startDownload 在开始时返回。此外,进度条的值不会重置为 0。

关于ios - swift iOS - NSURLSession 无法正确下载多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32799180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com