Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to cancel all request from Alamofire

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.86k
    Comment on it

    Hi Readers,

    If you are using Alamofire framework to implement API service classes and you need to cancel all the request which are fetching data from server. You can use following Swift code snippet:

    if #available(iOS 9.0, *) {
                Alamofire.Manager.session.getAllTasksWithCompletionHandler { (tasks) -> Void in
                    tasks.forEach({ $0.cancel() })
                }
            } else {
                // Fallback on earlier versions
                Alamofire.Manager.session.getTasksWithCompletionHandler({
                    $0.0.forEach({ $0.cancel() })
                    $0.1.forEach({ $0.cancel() })
                    $0.2.forEach({ $0.cancel() })
                })
            }

    Now if we take a closer look, we have condition here for iOS 9. In iOS 9 we have get all request either those are outstanding data, upload and download tasks at once and we get completion handler and into it we cancel all request. While in the else condition we are managing iOS 8 or previous versions. In that condition we get tasks completion handler which gives us three different array of outstanding data, upload and download tasks. By which we enumerate all kind of tasks and cancel it.

    Hope it will help. Thanks for reading.

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: