2016-11-27 55 views
1

基本上我有張貼簡單的數據到服務器的代碼,並會返回一個布爾值success如果POST請求是成功的,但它似乎是布爾值之前甚至返回數據被處理,我做錯了什麼?斯威夫特函數返回值之前完成處理

public func postRequest(rawText: String) -> Bool { 
    var success = true 
    let destUrl = "http://api.getquesto.com:8080/upload/" 
    var request = URLRequest(url: URL(string: destUrl)!) 
    request.httpMethod = "POST" 
    let postString = rawText 
    request.setValue("text/plain", forHTTPHeaderField: "Content-Type") 
// request.setValue("compute", forHTTPHeaderField: "Questo-Query") 
// request.setValue("Fuck you", forHTTPHeaderField: "quizTitle") 

    request.httpBody = postString.data(using: .utf8) 
    print(request.httpBody!) 
    let task = URLSession.shared.dataTask(with: request) { data, response, error in 
     guard let data = data, error == nil else {             // check for fundamental networking error 
      print("error=\(error)") 
      success = false 
      print(success) 

      return 
     } 

     if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {   // check for http errors 
      print("statusCode should be 200, but is \(httpStatus.statusCode)") 
      print("response = \(response)") 
     } 

     let responseString = String(data: data, encoding: .utf8) 
     do { 
      if let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions(rawValue: UInt(0))) as? [String: Any] { 
       print("json \(json)") 
      } else { 
       print("can not cast data") 
       success = false 

      } 
     } catch let error { 
      print("cant parse json \(error)") 
      success = false 
      print(success) 

     } 

     print("responseString = \(responseString)") 
     let dataString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) 
     //print(dataString) 
     //print("responseString = \(responseString)") 
     constantVariables.rawQuestionData = dataString as! String 
     let processedResults = dataString?.replacingOccurrences(of: "\\n", with: " ") 
     print("processed results = " + (processedResults! as String)) 
     let newArray = processedResults!.components(separatedBy: ", \"") 
     //print(newArray) 

     for index in 0...(newArray.count - 1) { 
      if index%2 == 0 { 
       constantVariables.questions.insert(newArray[index].replacingOccurrences(of: "\"", with: "").replacingOccurrences(of: "]", with: "").replacingOccurrences(of: "[", with: ""), at: index/2) 

       //  ConstantsArray.questionArray.append(newArray[index]) 
       //  print("question array: " + ConstantsArray.answerArray[index/2]) 
      }else{ 
       constantVariables.answers.insert(newArray[index].replacingOccurrences(of: "\"", with: "").replacingOccurrences(of: "]", with: "").replacingOccurrences(of: "[", with: ""), at: (index-1)/2) 

       //  ConstantsArray.questionArray.append(newArray[index]) 
       print("answer array: " + constantVariables.answers[(index-1)/2]) 

      } 
     } 
    } 
    task.resume() 
    print(success) 
    return success 
    } 
+2

'URLSessionDataTask'是異步的,當你調用'簡歷()'你的代碼會繼續和任務兩種並行線程開始。如果你希望它是同步的,使類似於此代碼的擴展:http://stackoverflow.com/a/34308158/2122979 – Ossir

+0

的答案更多類似的問題:http://stackoverflow.com/questions/40810108/swift -http請求使用-urlsession,http://stackoverflow.com/questions/40811500/ios-swift-value-of-an-integer-is-not-being-saved,http://stackoverflow.com/questions/40756123/SWIFT-確保-urlsession-datatask-被完成的 - 在 - 我 - 功能 - 前扯皮清晰度,http://stackoverflow.com/questions/40491502/swift-3-send-make-synchronous-http - 請求,... –

回答

2

發生這種情況,因爲該函數直接返回的success值,dataTask工作異步,因此,該函數不應該等到dataTask完成解析編輯的success的值,即:return success執行之前dataTask編輯success的值。

我建議讓函數處理一個completion而不是直接返回Bool

你的功能應該是類似於:

public func postRequest(rawText: String, completion: @escaping (_ success: Bool) ->()) { 
    var success = true 
    let destUrl = "http://api.getquesto.com:8080/upload/" 
    var request = URLRequest(url: URL(string: destUrl)!) 
    request.httpMethod = "POST" 
    let postString = rawText 
    request.setValue("text/plain", forHTTPHeaderField: "Content-Type") 
    // request.setValue("compute", forHTTPHeaderField: "Questo-Query") 
    // request.setValue("Fuck you", forHTTPHeaderField: "quizTitle") 

    request.httpBody = postString.data(using: .utf8) 
    print(request.httpBody!) 
    let task = URLSession.shared.dataTask(with: request) { data, response, error in 
     guard let data = data, error == nil else {             // check for fundamental networking error 
      print("error=\(error)") 
      success = false 
      print(success) 

      return 
     } 

     if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {   // check for http errors 
      print("statusCode should be 200, but is \(httpStatus.statusCode)") 
      print("response = \(response)") 
     } 

     let responseString = String(data: data, encoding: .utf8) 
     do { 
      if let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions(rawValue: UInt(0))) as? [String: Any] { 
       print("json \(json)") 
      } else { 
       print("can not cast data") 
       success = false 

      } 
     } catch let error { 
      print("cant parse json \(error)") 
      success = false 
      print(success) 

     } 

     print("responseString = \(responseString)") 
     let dataString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) 
     //print(dataString) 
     //print("responseString = \(responseString)") 
     constantVariables.rawQuestionData = dataString as! String 
     let processedResults = dataString?.replacingOccurrences(of: "\\n", with: " ") 
     print("processed results = " + (processedResults! as String)) 
     let newArray = processedResults!.components(separatedBy: ", \"") 
     //print(newArray) 

     for index in 0...(newArray.count - 1) { 
      if index%2 == 0 { 
       constantVariables.questions.insert(newArray[index].replacingOccurrences(of: "\"", with: "").replacingOccurrences(of: "]", with: "").replacingOccurrences(of: "[", with: ""), at: index/2) 

       //  ConstantsArray.questionArray.append(newArray[index]) 
       //  print("question array: " + ConstantsArray.answerArray[index/2]) 
      }else{ 
       constantVariables.answers.insert(newArray[index].replacingOccurrences(of: "\"", with: "").replacingOccurrences(of: "]", with: "").replacingOccurrences(of: "[", with: ""), at: (index-1)/2) 

       //  ConstantsArray.questionArray.append(newArray[index]) 
       print("answer array: " + constantVariables.answers[(index-1)/2]) 

      } 
     } 

     completion(success) 
    } 
    task.resume() 
    print(success) 
} 

在斯威夫特3,你應該使用@escaping,以獲取更多信息,您可能要檢查this answer

呼叫:

postRequest(rawText: "rawText", completion: { success in 
    print(success) 
}) 

現在,它應該等待,直到dataTask結束它的解析,然後,在completion的代碼將被調用。

希望它幫助。

+0

非常感謝!這效果很好! –