Alamofire の共有マネージャーですべてのリクエストをキャンセルするには?
これは私の機能です:
class func cancelAllRequests() {
Alamofire.Manager.sharedInstance.session.getTasksWithCompletionHandler { (dataTasks, uploadTasks, downloadTasks) in
self.each(dataTasks, function: self.cancel)
self.each(uploadTasks, function: self.cancel)
self.each(downloadTasks, function: self.cancel)
}
}
class func cancel(index: Int, element: AnyObject) {
if let task = element as? NSURLSessionTask {
task.cancel()
}
}
class func each<T>(array: [T], function: (Int, T) -> ()) {
for (index, t) in enumerate(array) {
function(index, array[index])
}
}
それは機能しますか?