title: Instantly update front end with new array value
body: <p>I'm creating an app where users will send messages.  I have the messenger working but the front end isn't updating instantly when a new comment is entered.  You have to back out and then come back in to get it to show up.</p>
  
  <p>I've researched examples and got the code from a course I took on udemy.  I've tried with tableview.reload() and tableview.insertrows().  I'm sending the data using JSON to PHP and MySQL.  It's showing in the db immediately but my code has it updating the UI first, it's just not working.</p>
  
  <p>let messagetext = replyTxt.text.trimmingCharacters(in: .whitespacesAndNewlines)</p>
  
  <pre><code>    hhmessages.insert(messagetext as AnyObject, at: hhmessages.endIndex)
      let indexPath = IndexPath(row: self.hhmessages.count - 1, section: 0)
  
      // update tableView
      tableView.beginUpdates()
  
      tableView.insertRows(at: [indexPath], with: .automatic)
      tableView.endUpdates()
      // scroll to the bottom
      tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
  
      // empty textView and reload it
      replyTxt.text = ""
      textViewDidChange(replyTxt)
      let recipient = messages["username"] as! String
      let rid = String(describing: messages["recipient_id"]!)
      let uuid = messages["uuid"] as! String
      puuid = UUID().uuidString
  
      // prepare request
      let url = URL(string: "http://localhost/messagepost.php")!
      let body = "sender_id=\(user_id)&amp;sender=\(username)&amp;text=\(messagetext)&amp;recipient_id=\(rid)&amp;recipient=\(recipient)&amp;uuid=\(uuid)&amp;puuid=\(puuid)"
  
      var request = URLRequest(url: url)
      request.httpMethod = "POST"
      request.httpBody = body.data(using: .utf8)
  </code></pre>
  
  <p>The code should instantly update the UITableView with the latest comment and then commit to the database.  I've been trying for two days with no luck.  Any help is welcomed.</p>
  
  <p>UPDATE WITH TABLE SOURCE CODE</p>
  
  <pre><code> func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return hhmessages.count
  }
     // Return cell to display on the tableView
  // How many sections. Means numberOfSections * rows = view
  func numberOfSections(in tableView: UITableView) -&gt; Int {
      return 1
  }
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let colorSmoothGray = UIColor(red: 229/255, green: 229/255, blue: 234/255, alpha: 1)
      let colorBrandBlue = UIColor(red: 148 / 255, green: 33 / 255, blue: 147 / 255, alpha: 1)
  
      let pictureURL = hhmessages[indexPath.row]["uploadpath"] as? String
  
      // no picture in the post
      if pictureURL?.isEmpty == true {
          let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ConversationCell
          cell.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi));
  
          isLoading = true
  
          let hhpost = hhmessages[indexPath.row]
          let smimages = hhpost["path"] as? UIImage
          let text = hhmessages[indexPath.row]["messagetext"] as! String
          cell.messageLbl.text = text
          cell.smavaImg.image = smimages
  
              cell.messageLbl.textAlignment = .right
              cell.messageLbl.backgroundColor = colorSmoothGray
              cell.messageLbl.textColor = .black
              cell.messageLbl.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)
              cell.messageLbl.font?.withSize(25)
              cell.messageLbl.clipsToBounds = true
                // get main queue to this block of code to communicate back
     DispatchQueue.main.async {
  
          cell.messageLbl.sizeToFit()
          tableView.transform = CGAffineTransform(rotationAngle: -CGFloat.pi)
          cell.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
  
      }
          return cell
      } else {
          let cell = tableView.dequeueReusableCell(withIdentifier: "PicCell", for: indexPath) as! PicConversationCell
          cell.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi));
  
          cell.smavaImg.image = smimages
          //if let message = messageData {
          for i in 0 ..&lt; self.incoming.count {
              // Confiture the constraints for cell
              if self.incoming[indexPath.row] == 1 {
                                   // Constraints
                 cell.lefBubbleConstraint.isActive = true
                  cell.rightBubbleConstraint.isActive = false
                  if cell.postpictureImg.image == nil {
                      cell.postpictureImg.backgroundColor = colorwhite
                      cell.postpictureImg.clipsToBounds = true
  
                  }
                  else {
                      cell.postpictureImg.backgroundColor = .clear
                      cell.postpictureImg.clipsToBounds = true
  
                  }
  
              }
              else if self.incoming[indexPath.row] == 0 {
  
  
                  // Constraints
                  cell.lefBubbleConstraint.isActive = false
                  cell.rightBubbleConstraint.isActive = true
                  if cell.postpictureImg.image == nil {
                      cell.postpictureImg.backgroundColor = colorwhite
                      cell.postpictureImg.clipsToBounds = true
  
                  }
                  else {
                      cell.postpictureImg.backgroundColor = .clear
                      cell.postpictureImg.clipsToBounds = true
  
                  }
  
              }
  
          // pictures logic
          let pictureString = hhmessages[indexPath.row]["uploadpath"] as? String
          let pictureURL = URL(string: pictureString!)!
  
          // if there are still pictures to be loaded
          if hhmessages.count != pictures.count {
  
              URLSession(configuration: .default).dataTask(with: pictureURL) { (data, response, error) in
                                    // downloaded
                  if let image = UIImage(data: data!) {
  
                      self.pictures.append(image)
  
                      DispatchQueue.main.async {
                          cell.postpictureImg.image = image
                      }
                  }
  
                  }.resume()
  
              // cached picture
          } else {
  
              DispatchQueue.main.async {
                  cell.postpictureImg.image = self.pictures[indexPath.row]
              }
          }
  
          }
  
          return cell
  
      }
  
  }
  </code></pre>