我想向我所拥有的Web服务器发出多个POST
请求,这些请求会在我的数据库的表中插入新记录。这将根据用户输入的练习量重复进行。
我有POST
请求的功能,如下所示。
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()
}
这是在循环中调用的,
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!)
}
目前,仅用户输入数据的第一个练习被插入到数据库中。好像它正在执行所有代码太快。我希望有人理解这一点,并能帮助我!
答案 0 :(得分:1)
阅读: 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