Cancel a request Alamofire

前端 未结 5 1023
青春惊慌失措
青春惊慌失措 2020-12-23 21:14

I am sending a request which is triggered based on timer. But if I press the back button the request still seems to be active and the response in turns crashes the app. Kind

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-23 21:37

    did you try this solution:

    let sessionManager = Alamofire.SessionManager.default 
    sessionManager.session.getTasksWithCompletionHandler { dataTasks, uploadTasks, downloadTasks in 
    dataTasks.forEach { $0.cancel() } 
    uploadTasks.forEach { $0.cancel() } 
    downloadTasks.forEach { $0.cancel() } 
    }
    

    i also add a check to verify if is this the request that i want to cancel:

    dataTasks.forEach
                {
                    if ($0.originalRequest?.url?.absoluteString == url)
                    {
                        $0.cancel()
                    }
                }
    

提交回复
热议问题