iOS Alamofire stop all requests

前端 未结 8 1557
刺人心
刺人心 2020-12-05 07:45

Is there any way I can for example say:

Alamofire.Manager.cancelAllRequests() or Alamofire.Manager.sharedInstance.cancelAllRequests()?

相关标签:
8条回答
  • 2020-12-05 08:04

    You should use the NSURLSession methods directly to accomplish this.

    Alamofire.SessionManager.default.session.invalidateAndCancel()
    

    This will call all your completion handlers with cancellation errors. If you need to be able to resume downloads, then you'll need to grab the resumeData from the request if it is available. Then use the resume data to resume the request in place when you're ready.

    0 讨论(0)
  • 2020-12-05 08:08

    in completion to the @Loïs Di Qual you can check the request url and cancel (suspend, resume) the request that you need:

    downloadTasks.forEach
                {
                    if ($0.originalRequest?.url?.absoluteString == url)
                    {
                        $0.cancel()
                    }
                }
    
    0 讨论(0)
  • 2020-12-05 08:08

    How to stop the Api calling Alomofire in swift

    class func StopAPICALL()  {
            let sessionManager = Alamofire.SessionManager.default
            sessionManager.session.getTasksWithCompletionHandler { dataTasks, uploadTasks, downloadTasks in
                dataTasks.forEach { $0.cancel() }
                uploadTasks.forEach { $0.cancel() }
                downloadTasks.forEach { $0.cancel() }
            }
        }
    
    0 讨论(0)
  • 2020-12-05 08:11

    cnoon's one-liner solution is great but it invalidates the NSURLSession and you need to create a new one.

    Another solution would be this (iOS 7+):

    session.getTasks { dataTasks, uploadTasks, downloadTasks in
        dataTasks.forEach { $0.cancel() }
        uploadTasks.forEach { $0.cancel() }
        downloadTasks.forEach { $0.cancel() }
    }
    

    Or if you target iOS 9+ only:

    session.getAllTasks { tasks in
        tasks.forEach { $0.cancel() }
    }
    
    0 讨论(0)
  • 2020-12-05 08:15

    If it helps, I got cnoon's answer to work on my own instance of an Alamofire.Manager. I have a singleton class called NetworkHelper which has a property called alamoFireManager, which handles all my network requests. I just call the NSURSession invalidateAndCancel() on that alamoFireManager property, reset my manager in setAFconfig(), then I'm good to go.

    class NetworkHelper {
    
    private var alamoFireManager : Alamofire.Manager!
    
    class var sharedInstance: NetworkHelper {
        struct Static {
            static var instance: NetworkHelper?
            static var token: dispatch_once_t = 0
        }
    
        dispatch_once(&Static.token) {
            Static.instance = NetworkHelper()
        }
    
        return Static.instance!
    }
    init(){
        setAFconfig()
    }
    
    func setAFconfig(){
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        configuration.timeoutIntervalForResource = 4
        configuration.timeoutIntervalForRequest = 4
        alamoFireManager = Alamofire.Manager(configuration: configuration)
    }
    func cancelAllRequests() {
        print("cancelling NetworkHelper requests")
        alamoFireManager.session.invalidateAndCancel()
        setAFconfig()
    }
    
    0 讨论(0)
  • 2020-12-05 08:17

    Below Code stops the Requests in [Swift 3]:

    Plus the code works for Alamofire v3 & v4 plus for iOS 8+.

    func stopTheDamnRequests(){
        if #available(iOS 9.0, *) {
            Alamofire.SessionManager.default.session.getAllTasks { (tasks) in
                tasks.forEach{ $0.cancel() }
            }
        } else {
            Alamofire.SessionManager.default.session.getTasksWithCompletionHandler { (sessionDataTask, uploadData, downloadData) in
                sessionDataTask.forEach { $0.cancel() }
                uploadData.forEach { $0.cancel() }
                downloadData.forEach { $0.cancel() }
            }
        }
    }
    

    Simply Copy and paste the function.

    0 讨论(0)
提交回复
热议问题