我有问题。我正在尝试使用NSURLSession进行多次下载,但我没有得到我做错了什么?该类在类X中初始化一次.startDownload(realm.objects(Music)[indexPath.row])可以在同一个类中多次调用。 “下载”课程中的问题,我肯定知道。如果您需要更多信息,请写下
class Download: NSObject, NSURLSessionDelegate {
var progress: Float = 0.0
var progressBar: UIProgressView?
var addButton: UIButton?
private var downloadTask: [NSURLSessionDownloadTask] = []
private var backgroundSession: [NSURLSession] = []
private let realm = try! Realm()
private var downloadObject:[Music] = []
private var queueObjects:[Music] = []
func startDownload(object: Music? = nil) {
if (object != nil) {
self.queueObjects.append(object!)
}
let url = queueObjects[queueObjects.startIndex].url
if downloadTask.count < 3 {
let backgroundSessionConfiguration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("backgroundSession"+String(queueObjects.count))
backgroundSession.append(NSURLSession(configuration: backgroundSessionConfiguration, delegate: self, delegateQueue: NSOperationQueue.mainQueue()))
let sessionIndex = backgroundSession.endIndex-1
backgroundSession[sessionIndex].sessionDescription = String(sessionIndex)
downloadTask.append(backgroundSession[sessionIndex].downloadTaskWithURL(NSURL(string: url)!))
let taskIndex = downloadTask.endIndex-1
downloadTask[taskIndex].taskDescription = String(taskIndex)
downloadTask[taskIndex].resume()
downloadObject.append(queueObjects[queueObjects.startIndex])
queueObjects.removeAtIndex(queueObjects.startIndex)
}
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
let index = Int(downloadTask.taskDescription!)!
print("Index "+String(index))
let range = downloadObject[ index ].url.rangeOfString("?")!.startIndex.advancedBy(0)
let url = downloadObject[ index ].url[downloadObject[index].url.startIndex..<range]
let theFileName = (url as NSString).lastPathComponent
let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let directoryPath:String = path[0]
let fileManager = NSFileManager()
let destinationURLForFile = NSURL(fileURLWithPath: directoryPath.stringByAppendingString( "/"+theFileName))
if fileManager.fileExistsAtPath(destinationURLForFile.path!){
print(destinationURLForFile.path!)
saveObject(downloadObject[index], path: destinationURLForFile.path!)
}
else{
do {
try fileManager.moveItemAtURL(location, toURL: destinationURLForFile)
print(destinationURLForFile.path!)
saveObject(downloadObject[index], path: destinationURLForFile.path!)
} catch {
print("An error occurred while moving file to destination url")
}
}
if addButton != nil {
addButton?.hidden = true
}
downloadTask.cancel()
session.invalidateAndCancel()
self.backgroundSession[Int(session.sessionDescription!)!].invalidateAndCancel()
self.backgroundSession.removeAtIndex(Int(session.sessionDescription!)!)
self.downloadTask[Int(downloadTask.taskDescription!)!].cancel()
self.downloadTask.removeAtIndex(Int(downloadTask.taskDescription!)!)
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
progress = Float(totalBytesWritten)/Float(totalBytesExpectedToWrite)
if progressBar != nil {
progressBar?.progress = progress
}
}
private func saveObject(object: Music, path: String) {
let downloadMusic = DownloadMusic()
downloadMusic.id = object.id
downloadMusic.owner_id = object.owner_id
downloadMusic.artist = object.artist
downloadMusic.title = object.title
downloadMusic.duration = object.duration
downloadMusic.path = path
try! realm.write() {
realm.add(downloadMusic)
downloadObject.removeAtIndex(downloadObject.endIndex-1)
if self.queueObjects.count > 0 {
self.startDownload()
}
print(queueObjects.count)
print(downloadObject.count)
print(downloadMusic)
}
}
}
谢谢
答案 0 :(得分:0)
首先,不要这样做。让会话限制你的并发性。立即抛出所有请求。
其次,除非你的应用刚刚启动,否则不要重新创建后台会话配置。你应该创建一次,而不是再创建它。指向相同标识符的多个NSURLSession对象的行为是IIRC,未定义。
第三,在完成会话之前,请不要使会话无效。您在第一个请求完成后立即取消所有未完成的请求。
第四,除非你想要停止正在进行的任务,否则你不应该取消任务。如果任务已经完成,取消它什么都不做。
除此之外,我还要同意那些说我需要解释代码错误的人才能进一步提供帮助。 : - )