title: How can I slow down a program to make the request to the server?
body: <p>I need to know that the server is returned to display in a cell right inoformatsiya after editing or return the previous line, if an error occurs. Or I can get data asynchronously, and later to update the results of a cell?</p>
  
  <p>The first press of the button allows the editing, the second store information on a server and object-question</p>
  
  <pre><code>var tempText:String!
          @IBAction func editButtonTapped(_ sender:UIButton) {
              print("editButtonTapped")
              textIsEditable = !textIsEditable
  
  
              if textIsEditable == true {
                  tempText = questionTextView.text
                  questionTextView.isEditable=true
                  questionTextView.backgroundColor = UIColor.white
              } else {
                  questionTextView.isEditable=false
                  questionTextView.backgroundColor = UIColor.clear
  
                  question.questionText=questionTextView.text
  
                  //Edit question on the server
                  if question.editQuestion() == true {
                      print("return true")
                      if delegate != nil {
                          //delegate.editQuestionAction(question: question)
                          delegate.editQuestionAction(cell: self)
  
                      }
                  } else {
                      questionTextView.text = tempText
                      question.questionText = tempText
                  }
              }
  
          }
  </code></pre>
  
  <p>Method in Question class for server request:</p>
  
  <pre><code>func editQuestion() -&gt; Bool {
          var edited=false
  
          //Prepare image for put
          let stringImage:String
          if questionImage == nil {
              stringImage=""
          } else {
              stringImage=imageName
          }
  
          let editDict:[String:String] = ["category" : category,
                         "text" : questionText,
                         "image": stringImage,
                         "id" : questionId]
  
          Connection.fetchData(feed: "quests", token: nil, parameters: editDict as [String : AnyObject]?, method: "PUT") { (result, responseDict) in
              if let success = responseDict?["success"] as? String {
                  if success == "1" {
                      edited = true
                  } else {
                      edited = false
                  }
              }
          }
          return edited
      }
  </code></pre>
  
  <p>Method for request to the server:</p>
  
  <pre><code>static func fetchData(feed:String,token:String? = nil,parameters:[String:AnyObject]? = nil,method:String? = nil, onCompletion:@escaping (_ success:Bool,_ data:NSDictionary?)-&gt;Void){
  
          DispatchQueue.main.async() {
              UIApplication.shared.isNetworkActivityIndicatorVisible = true
  
              //let url = NSURL(string: feed)
              if let unwrapped_url = NSURL(string: serverString+feed){
  
                  let request = NSMutableURLRequest(url: unwrapped_url as URL)
  
                  if let tk = token {
                      let authValue = "Token \(tk)"
                      request.setValue(authValue, forHTTPHeaderField: "Authorization")
                  }
  
                  if let parm = parameters{
                      do {
                          if let data = try JSONSerialization.data(withJSONObject: parm, options:[]) as NSData? {
  
                              //println(NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: nil))
                              request.httpBody = data as Data
                              request.setValue("application/json", forHTTPHeaderField: "Content-Type")
                              request.setValue("\(data.length)", forHTTPHeaderField: "Content-Length")
                          }
                      } catch let error as NSError {
                          print(error)
                      }
                  }
  
                  if let unwrapped_method = method {
                      request.httpMethod = unwrapped_method
                  }
  
                  let sessionConfiguration = URLSessionConfiguration.default
                  sessionConfiguration.timeoutIntervalForRequest = 15.0
                  let session = URLSession(configuration: sessionConfiguration)
                  let taskGetCategories = session.dataTask(with: request as URLRequest){ (responseData, response, error) -&gt; Void in
  
                      let statusCode = (response as! HTTPURLResponse?)?.statusCode
                      print("Status Code: \(statusCode), error: \(error)")
                      if error != nil || (statusCode != 200 &amp;&amp; statusCode != 201 &amp;&amp; statusCode != 202){
                          onCompletion(false, nil)
  
                      }
                      else {
                          do {
                              if let dictionary = try JSONSerialization.jsonObject(with: responseData!, options: [.mutableContainers, .allowFragments]) as? NSDictionary{
                                  onCompletion(true,dictionary)
  
                              } else{
                                  onCompletion(false, nil)
                              }
                          } catch let error as NSError {
                              print(error)
                          }
                      }
                  }
  
                  UIApplication.shared.isNetworkActivityIndicatorVisible = false
                  taskGetCategories.resume()
              }
          }
      }
  </code></pre>
  
  <p>UPDATE(import SwiftHTTP, need ios8):</p>
  
  <pre><code>func editQuestion(completion:@escaping (Bool)-&gt;()) {
          var edited=false
  
          //Prepare image for put
          let stringImage:String
          if questionImage == nil {
              stringImage=""
          } else {
              stringImage=imageName
          }
  
          let editDict:[String:String] = ["category" : category,
                         "text" : questionText,
                         "image": stringImage,
                         "id" : questionId]
  
          do {
              let opt = try HTTP.PUT(serverString+"quests", parameters: editDict)
              opt.start { response in
                  //do things...
                  if let err = response.error {
                      print("error: \(err.localizedDescription)")
                      DispatchQueue.main.async {
                          completion(edited)
                      }
                      return //also notify app of failure as needed
                  }
                  let responseDict=convertStringToDictionary(text: response.text!)
                  if let success = responseDict?["success"] as? String {
                      if success == "1" {
                          edited = true
                          completion(edited)
                      } else {
                          edited = false
                          completion(edited)
                      }
                  }
              }
          } catch let error {
              print("got an error creating the request: \(error)")
          }
      }
  </code></pre>
  
  <p>now good?</p>