如何在从Alamofire v3迁移到v4后取消所有请求?

时间:2016-10-19 11:09:19

标签: swift alamofire

我使用此代码取消所有Alamofire次请求:

func stopAllSessions() {
    Alamofire.Manager.sharedInstance.session.getAllTasksWithCompletionHandler { tasks in
        tasks.forEach { $0.cancel() }
    }
}

快速移植3后,这不起作用,我无法在getAllTasksWithCompletionHandler SessionManager 4中找到Alamofire方法。

2 个答案:

答案 0 :(得分:8)

实测值:

    Alamofire.SessionManager.default.session.getTasksWithCompletionHandler { (sessionDataTask, uploadData, downloadData) in
        sessionDataTask.forEach { $0.cancel() }
        uploadData.forEach { $0.cancel() }
        downloadData.forEach { $0.cancel() }
    }

答案 1 :(得分:0)

您要搜索的方法已更改为getAllTasks(completionHandler:)

Alamofire.SessionManager.default.session.getAllTasks { task in
    task.forEach { $0.cancel() }
}