未调用NSURLSession委托

use*_*220 18 ios swift

在以下代码中,文件下载得很好.但是,似乎没有调用任何委托方法,因为我没有收到任何输出.progressView也没有更新.知道为什么吗?

import Foundation
import UIKit

class Podcast: PFQueryTableViewController, UINavigationControllerDelegate, MWFeedParserDelegate, UITableViewDataSource, NSURLSessionDelegate, NSURLSessionDownloadDelegate {

    func downloadEpisodeWithFeedItem(episodeURL: NSURL) {

    var request: NSURLRequest = NSURLRequest(URL: episodeURL)
    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)

    var downloadTask = session.downloadTaskWithURL(episodeURL, completionHandler: { (url, response, error) -> Void in
        println("task completed")
        if (error != nil) {
            println(error.localizedDescription)
        } else {
            println("no error")
            println(response)
        }
    })
    downloadTask.resume()

}

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
    println("didResumeAtOffset")
}

    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
          var downloadProgress = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)
    println(Float(downloadProgress))
    println("sup")

    epCell.progressView.progress = Float(downloadProgress)
}

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

}
}
Run Code Online (Sandbox Code Playgroud)

Nat*_*ook 29

从我的测试中,您必须选择是否要使用委托或完成处理程序 - 如果同时指定两者,则只调用完成处理程序.这段代码给了我运行进度更新和didFinishDownloadingToURL事件:

func downloadEpisodeWithFeedItem(episodeURL: NSURL) {
    let request: NSURLRequest = NSURLRequest(URL: episodeURL)
    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config, delegate: self, delegateQueue: NSOperationQueue.mainQueue())

    let downloadTask = session.downloadTaskWithURL(episodeURL)
    downloadTask.resume()
}

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
    println("didResumeAtOffset: \(fileOffset)")
}

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
    var downloadProgress = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)
    println("downloadProgress: \(downloadProgress)")
}

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
    println("didFinishDownloadingToURL: \(location)")
    println(downloadTask)
}
Run Code Online (Sandbox Code Playgroud)

NSURLSession文档中,这是相关部分:

与大多数网络API一样,NSURLSession API是高度异步的.它以两种方式之一返回数据,具体取决于您调用的方法:

  • 到传输成功或出错时将数据返回到应用程序的完成处理程序块.
  • 通过在收到数据时调用自定义委托上的方法.
  • 通过在下载到文件完成时调用自定义委托上的方法.

因此,通过设计它返回的数据或者完成处理程序块委托.但正如在这里所表明的那样,并非两者都有.

  • 它只是一个有趣的API细节,处理程序覆盖委托:) (2认同)
  • "有趣"是一种表达方式!长期以来一直困惑于此.为什么我不能使用完成处理程序,例如,同时通过委托管理缓存行为?看起来很奇怪. (2认同)

Man*_*ban 18

有趣的是,Apple特别解释了他们的这种行为NSURLSessionDataDelegate(但在基本代表中NSURLSessionTaskDelegate也没有NSURLSessionDownloadDelegate)

注意

NSURLSession对象不需要委托.如果未分配任何委托,则在该会话中创建任务时,必须提供完成处理程序块以获取数据.

完成处理程序块主要用于替代使用自定义委托.如果使用采用完成处理程序块的方法创建任务,则不会调用响应和数据传递的委托方法.

  • 哇,这应该突出显示。感谢你。 (2认同)