我想取消一些请求,但不是全部。现在我这样做:
func cancelAllRequests() {
let sessionManager = Alamofire.SessionManager.default
sessionManager.session.getTasksWithCompletionHandler { dataTasks, uploadTasks, downloadTasks in
dataTasks.forEach { $0.cancel() }
uploadTasks.forEach { $0.cancel() }
downloadTasks.forEach { $0.cancel() }
}
}
,但它取消了所有请求。我怎么能区分请求?
如果您知道请求或网址,您可以写这样的内容来取消特定任务
func cancellRequest(for request: URLRequest) {
Alamofire.SessionManager.default.session.getAllTasks { (tasks) in
_ = tasks
.filter({ $0.originalRequest?.url?.path == request.url?.path })
.map({ $0.cancel() })
}
}