Home Multiple calls simultaneously to an API in Alamofire, Swift
Reply: 0

Multiple calls simultaneously to an API in Alamofire, Swift

kinza
1#
kinza Published in 2017-12-07 05:28:24Z

I am having a tableview in my app as shown:

What I need is when a user tap the heart image, request goes to the sever to alter status of that tableview item, and the response (success or error) should come back. Here is the code which gets called on tap:

var manager = Alamofire.SessionManager.default
manager = Alamofire.SessionManager(configuration: configuration)
manager.request(######, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON(completionHandler: { response in

                switch response.result {
                case .success:
                    print(response)

                    let responseJson = JSON(response.result.value!)

                    // parsing JSON response using swiftyJSON
                    let code = (responseJson["code"]).int
                    let successStatus = responseJson["success"]
                    let message = (responseJson["message"]).rawString()

                    if ((code == 200) && (successStatus == true))
                    {
                        success()
                    }
                    else if (successStatus == false)
                    {
                        let errorObj = CError()
                        if ((code == 401) || (code == 404) || (code == 409))
                        {
                            errorObj.message = message!
                            errorObj.code = code!
                            error(errorObj)
                        }

                        else if ((code == 400) || (code == 500))
                        {
                            let object = response.result.value as? [String:Any]
                            let responseErrors = object!["errors"] as? [String:Any]
                            errorObj.responseErrors = responseErrors
                            errorObj.message = message!
                            errorObj.code = code!
                            error(errorObj)
                        }
                        else
                        {
                            errorObj.message = "Error processing request"
                            errorObj.code = 500
                            error(errorObj)
                        }
                    }

                case .failure (let serverError):
                    let errorObj = CError()
                    if (serverError._code == NSURLErrorTimedOut)
                    {
                        errorObj.message = "Request timed out"
                    }
                    else
                    {
                        errorObj.message = "Error sending request to server"
                    }
                    errorObj.code = 500
                    error(errorObj)
                }
            }
            )

Everything goes well if i tap only one item. But when I tap more than one items, suppose I have tapped two items, error block of the code gets called:

case .failure (let serverError):
                        let errorObj = CError()
                        if (serverError._code == NSURLErrorTimedOut)
                        {
                            errorObj.message = "Request timed out"
                        }
                        else
                        {
                            errorObj.message = "Error sending request to server" //this line gets called
                        }
                        errorObj.code = 500
                        error(errorObj)
                    }

and after that success response for one item comes back.

What I have understood from current situation is:

  • One call to server goes.
  • Its response has not come back and second call goes.
  • This call blocks the first request and its error block is called.
  • Then second call takes time to get processed and its response comes back (success block gets called)

According to documentation of Alamofire it's calls are processed in background. But according to this https://github.com/Alamofire/Alamofire/issues/1922 the response by default gets called on the main thread. And I think this is the problem why the simultaneous are getting blocked. In order to process in background, I tried this approach:

let queue = DispatchQueue(label: "favAdv", qos: .background, attributes: .concurrent)
manager.request(urlAdventureFavouriteStatus, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON(queue: queue) { response in .....

But this didn't work for me. Can anyone suggest me solution for this problem? If anything is wrong in my understanding then correct it because I am new to iOS.

I am using Xcode 9.1, Swift 4, Alamofire 4.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301215 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO