我怎么能用Alamofire取消几个请求?

问题描述 投票:0回答:1

我想取消一些请求,但不是全部。现在我这样做:

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() }

    }
}

,但它取消了所有请求。我怎么能区分请求?

ios request alamofire
1个回答
0
投票

如果您知道请求或网址,您可以写这样的内容来取消特定任务

func cancellRequest(for request: URLRequest) {
    Alamofire.SessionManager.default.session.getAllTasks { (tasks) in
        _ = tasks
            .filter({ $0.originalRequest?.url?.path == request.url?.path })
            .map({ $0.cancel() })
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.