I'd like to make multiple POST
requests to a web server that I have got which inserts a new record in a table in my database. This will be repeated depending on the amount of exercises the user inputs.
I have a function for the POST
request which is as follows.
func submitDetails(split_id:Int, day:String, name:String, set:String, rep:String)
{
var request = URLRequest(url: URL(string: "LINK OF WEB SERVICE")! as URL)
request.httpMethod = "POST"
let postString = "id=\(split_id)&day=\(day)&name=\(name)&sets=\(set)&reps=\(rep)"
print("Post string - \(postString)")
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest)
{
data, response, error in
if error != nil
{
print("error=\(String(describing: error))")
return
}
print("response = \(String(describing: response))")
let responseString = String(data: data!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
print ("responseString =\(String(describing: responseString))")
}
task.resume()
}
This is called within a loop,
for x in 0...MainMenuViewController.myVariables.day1NoExercise - 1
{
self.submitDetails(split_id: MainMenuViewController.myVariables.new_split_id, day: self.dayName.text!, name: self.exerciseName[x].text!, set: self.exerciseSets[x].text!, rep: self.exerciseReps[x].text!)
}
Currently, only the first exercise that the user inputs data for is inserted into the database. It seems like it is executing all the code too fast. I hope someone understands this and can help me out!