Ios NSOperationQueue的快速取消不起作用
我试图使用swift方法取消Ios NSOperationQueue的快速取消不起作用,ios,swift,Ios,Swift,我试图使用swift方法取消NSOperationQueue进程cancelAllOperations(),但它不起作用 我正在从url下载文件,但取消下载时,该文件不起作用 以下是我正在使用的代码: var s = NSOperationQueue.mainQueue() lazy var session : NSURLSession = { let config = NSURLSessionConfiguration.ephemeralSessionConfiguration()
NSOperationQueue
进程cancelAllOperations()
,但它不起作用
我正在从url
下载文件,但取消下载时,该文件不起作用
以下是我正在使用的代码:
var s = NSOperationQueue.mainQueue()
lazy var session : NSURLSession = {
let config = NSURLSessionConfiguration.ephemeralSessionConfiguration()
config.allowsCellularAccess = false
let session = NSURLSession(configuration: config, delegate: self, delegateQueue: self.s)
return session
}
override func viewDidLoad() {
progressBar.setProgress(0.0, animated: true) //set progressBar to 0 at start
}
@IBAction func cancel(sender: AnyObject) {
s.cancelAllOperations()
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten writ: Int64, totalBytesExpectedToWrite exp: Int64) {
print("downloaded \(100*writ/exp)")
dispatch_async(dispatch_get_main_queue(), {
self.counter = Float(100*writ/exp)
return
})
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
// unused in this example
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
print("completed: error: \(error)")
if(error != nil){
}
}
// this is the only required NSURLSessionDownloadDelegate method
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let documentDirectoryPath:String = path[0]
let fileManager = NSFileManager()
let destinationURLForFile = NSURL(fileURLWithPath: documentDirectoryPath.stringByAppendingString("Music.mp3"))
if fileManager.fileExistsAtPath(destinationURLForFile.path!){
print("File Already Exists")
}
else{
do {
try fileManager.moveItemAtURL(location, toURL: destinationURLForFile)
print("File doesn't exists")
}catch{
print("An error occurred while moving file to destination url")
}
}
}
在您的操作中,
cancel
的实现是什么样子的?查看cancelAllOperations()
的文档;您仍然需要做一些工作来处理操作中的调用。我正在尝试取消下载进度…我知道您正在尝试做什么-我在问您做了什么来支持您想要做的事情。我尝试使用task.cancel()取消下载进度,下载已被取消,但问题是单击“下载”按钮后不会再次开始下载。我不知道问题出在哪里我是iOS开发新手。在您的操作中,cancel
的实现是什么样子的?查看cancelAllOperations()
的文档;您仍然需要做一些工作来处理操作中的调用。我正在尝试取消下载进度…我知道您正在尝试做什么-我在问您做了什么来支持您想要做的事情。我尝试使用task.cancel()取消下载进度,下载已被取消,但问题是单击下载按钮后不会再次开始下载。我不知道问题出在哪里我是iOS开发新手。