如果用户在 swift iOS 中点击两次,则取消 NSURLSession
Cancel NSURLSession if user taps twice in swift iOS
我正在使用 NSURLSession,根据此线程,这是首选。
let url = NSURL(string: "http://www.whosebug.com")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
}
task.resume()
我唯一的问题是当用户点击两次并且我想取消第一个请求时。我试过 task.cancel() 但打印语句仍然执行(即在 .cancel() 之后出现 NSDomainError)。我如何安全地取消此请求 (NSURLSessionDataTask),而不触发 print 语句,或者甚至可能?
编辑:
只是为了清楚。 URL 可能相同,我想取消第一个请求。
我就是这样解决问题的。当执行 task.cancel() 时,打印语句将不会被执行。检查字符串似乎有点老套,所以如果有人有更好的解决方案,我会接受那个。
let url = NSURL(string: "http://www.whosebug.com")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
if (error?.userInfo?["NSLocalizedDescription"] as? String) == "cancelled"
{
return
}
println(NSString(data: data, encoding: NSUTF8StringEncoding))
}
task.resume()
我希望这是你可以使用的东西:
class ViewController: UIViewController , NSURLSessionDownloadDelegate {
var download : NSURLSessionDownloadTask?
var backgroundSession : NSURLSession?
override func viewDidLoad() {
super.viewDidLoad()
let sessionConfiguration : NSURLSessionConfiguration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("CustomBackgroundIdentifier")
backgroundSession = NSURLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
var btn = UIButton(frame: CGRectMake(0, 0, 100, 100))
btn.backgroundColor = UIColor.redColor()
btn.addTarget(self, action: "startDownload", forControlEvents: UIControlEvents.TouchDown)
self.view.addSubview(btn)
var btn2 = UIButton(frame: CGRectMake(0, 120, 100, 100))
btn2.backgroundColor = UIColor.blueColor()
btn2.addTarget(self, action: "cancelDownload", forControlEvents: UIControlEvents.TouchDown)
self.view.addSubview(btn2)
}
func startDownload() {
if download == nil {
if let url = NSURL(string: "http://www.whosebug.com") {
download = backgroundSession?.downloadTaskWithURL(url)
download?.resume()
}
} else {
resumeDownload()
}
}
func pauseDownload() {
if download != nil {
download?.suspend()
}
}
func resumeDownload() {
if download != nil {
download?.resume()
}
}
func cancelDownload() {
if download != nil {
download?.cancel()
}
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
println("Session %@ download task %@ finished downloading to URL %@\n",
session, downloadTask, location)
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
println("downloaded: %i",totalBytesWritten)
}
}
我正在使用 NSURLSession,根据此线程,这是首选。
let url = NSURL(string: "http://www.whosebug.com")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
}
task.resume()
我唯一的问题是当用户点击两次并且我想取消第一个请求时。我试过 task.cancel() 但打印语句仍然执行(即在 .cancel() 之后出现 NSDomainError)。我如何安全地取消此请求 (NSURLSessionDataTask),而不触发 print 语句,或者甚至可能?
编辑: 只是为了清楚。 URL 可能相同,我想取消第一个请求。
我就是这样解决问题的。当执行 task.cancel() 时,打印语句将不会被执行。检查字符串似乎有点老套,所以如果有人有更好的解决方案,我会接受那个。
let url = NSURL(string: "http://www.whosebug.com")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
if (error?.userInfo?["NSLocalizedDescription"] as? String) == "cancelled"
{
return
}
println(NSString(data: data, encoding: NSUTF8StringEncoding))
}
task.resume()
我希望这是你可以使用的东西:
class ViewController: UIViewController , NSURLSessionDownloadDelegate {
var download : NSURLSessionDownloadTask?
var backgroundSession : NSURLSession?
override func viewDidLoad() {
super.viewDidLoad()
let sessionConfiguration : NSURLSessionConfiguration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("CustomBackgroundIdentifier")
backgroundSession = NSURLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
var btn = UIButton(frame: CGRectMake(0, 0, 100, 100))
btn.backgroundColor = UIColor.redColor()
btn.addTarget(self, action: "startDownload", forControlEvents: UIControlEvents.TouchDown)
self.view.addSubview(btn)
var btn2 = UIButton(frame: CGRectMake(0, 120, 100, 100))
btn2.backgroundColor = UIColor.blueColor()
btn2.addTarget(self, action: "cancelDownload", forControlEvents: UIControlEvents.TouchDown)
self.view.addSubview(btn2)
}
func startDownload() {
if download == nil {
if let url = NSURL(string: "http://www.whosebug.com") {
download = backgroundSession?.downloadTaskWithURL(url)
download?.resume()
}
} else {
resumeDownload()
}
}
func pauseDownload() {
if download != nil {
download?.suspend()
}
}
func resumeDownload() {
if download != nil {
download?.resume()
}
}
func cancelDownload() {
if download != nil {
download?.cancel()
}
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
println("Session %@ download task %@ finished downloading to URL %@\n",
session, downloadTask, location)
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
println("downloaded: %i",totalBytesWritten)
}
}