Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel all web service calls from Alamofire

In my project there are 4 web services running in the background. I would like to stop all these services on logout from the current state without waiting for response. I have found this code for doing that

Alamofire.Manager.sharedInstance.session.invalidateAndCancel()

But after calling this, I am not able to make service calls again. Is there any way I can cancel all the running requests?

Here's my service calling code

func getAllCount(parameters: [String: AnyObject],completion: (success : Bool) -> Void) {

            PKHUD.sharedHUD.contentView = PKHUDTextView(text: "Loading...")
            PKHUD.sharedHUD.show()
            request = Alamofire.request(.POST, GlobalConstants.KGetAllCount, parameters: parameters, encoding:.JSON).responseJSON
                {
                    response in switch response.result {
                    case .Success(let JSON):
                        PKHUD.sharedHUD.hide()
                        print("Success with JSON: \(JSON)")
                        let status : NSString = JSON.valueForKey("status") as! String
                        if(status .isEqualToString("1")){
                            MyViewState.QJoined = JSON.valueForKeyPath("data.TotalJoinQueue") as! String
                            MyViewState.Qstarted = JSON.valueForKeyPath("data.TotalCreatedQueue") as! String
                            MyViewState.Bumps = JSON.valueForKeyPath("data.TotalBump") as! String
                            completion(success: true)

                            break
                        }else{
                            completion(success: false)
                            Helper.globalAlert(JSON.valueForKey("data") as! String)
                            break
                        }
                    case .Failure(let error):
                        PKHUD.sharedHUD.hide()
                        print("Request failed with error: \(error)")
                        completion(success: false)
                        break

                    }
            }
        }
like image 251
Janak Thakkar Avatar asked Dec 29 '15 06:12

Janak Thakkar


People also ask

What is Alamofire in iOS?

Alamofire is a networking library written in Swift. You use it to make HTTP(S) requests on iOS, macOS and other Apple platforms. For example, to post data to a web-based REST API or to download an image from a webserver. Alamofire has a convenient API built on top of URLSession (“URL Loading System”).

Is Alamofire needed?

Advantages of Using AlamofireUsing Alamofire will give a cleaner project. API call interactions (POST/GET/PUT/etc.) will be easier and more understable. Alamofire simplifies a number of common networking tasks that makes development faster and easier.

How do you cancel API call in swift 5?

You can explicitly cancel a task by calling its cancel() method. Any task can check Task.

Is Alamofire asynchronous?

Networking in Alamofire is done asynchronously.


1 Answers

As far as I can tell there is no obvious/easy way to recreate the session. The suggested way to handle this problem is to keep and array of requests static var requests = [Alamofire.Request?]() then when you want to stop all the requests you can call use request.cancel(). Using this approach you will have to append each request after creating it.

class func stopAllRequests(){
    for request in MyClass.requests{
        if let request = request{
            request.cancel()
        }
    }
}

Similar Issue

like image 62
Alex Pelletier Avatar answered Sep 29 '22 19:09

Alex Pelletier