Reputation: 555
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:
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.
Upvotes: 3
Views: 4198
Reputation: 555
I solved the problem later by adding:
let configuration = URLSessionConfiguration.default
configuration.httpMaximumConnectionsPerHost = 10 // change the number according to need
Adding this will be able to send 10 requests simultaneously.
Upvotes: 5