iOS Alamofire stop all requests

元气小坏坏 提交于 2019-12-17 15:44:24

问题


Is there any way I can for example say:

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

Of course it would be great if these requests especially in case of image download would only be paused for later when I'll cal the same URL but... I need at least to be able to cancel all requests at a global level. Some suggestions ?

In my app I have a wrapper above the Alamofire.request(.Post....) way of doing things so I would really appreciate not making me create or interact with Manager class in another way besides that specified above.


回答1:


You should use the NSURLSession methods directly to accomplish this.

Alamofire.Manager.sharedInstance.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.




回答2:


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



回答3:


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.




回答4:


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



回答5:


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



回答6:


In Swift 2.2

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



回答7:



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


来源:https://stackoverflow.com/questions/32999892/ios-alamofire-stop-all-requests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!