0

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!

Luke Varty
  • 61
  • 7

1 Answers1

1
  • for API Calls always run asynchronous request
  • use background thread to help your application remain responsive
  • use compilation block to display error
  • show progress bar or something like that to let the user know that you are doing something
  • add Extra function to your server to allow bulk posting "Reduce http sessions "

Read : https://medium.com/@sdrzn/networking-and-persistence-with-json-in-swift-4-c400ecab402d https://medium.com/@sdrzn/networking-and-persistence-with-json-in-swift-4-part-2-e4f35a606141

  • Advise : it's look like you taking your first steps with Swift/IOS so Just use http library like Alamofire to avoid all headache Like Queuing,Threading,Complitions Block .

https://github.com/Alamofire/Alamofire

Ayman Ferki
  • 196
  • 7
  • Using asynchronous request, to find out when that task has finished, would a progress bar be used. Then if the progress bar is not enabled then send data for a second `POST` request? – Luke Varty Mar 07 '18 at 10:34
  • don't use the progress bar to check the state . just use it to notify the user that something happen . i would like to recommend that you build bulk features on server side so you can send multiple posts on single request – Ayman Ferki Mar 08 '18 at 02:35
  • How do I go about implementing `build bulk features`? That sounds perfect for what I want to do. – Luke Varty Mar 09 '18 at 09:24
  • you need to build new feature on the Server side . the new method must accept json object . you can pass multiple element to your server to process in single request . check https://developer.salesforce.com/docs/atlas.en-us.api_asynch.meta/api_asynch/datafiles_json_sample_file.htm i can help if you able to share your API code / structure .. etc – Ayman Ferki Mar 09 '18 at 23:14