title: TableView does not load realm database values
body: <p>I have used <strong>XMPPFramework</strong>, since the user authentication and receiving only messages is done using XMPP. I am sending messages using <strong>RestApi</strong>. I have parsed the received xml message using <strong>SwiftyXMLParser</strong> library and now I have to store 'senderName', 'messageBody', 'time', 'senderNumber' in <strong>Realm</strong> database. </p>
  
  <p>Initially the tableView in ContactsViewController is empty. When user sends the message, It will first get stored in database and then I have to show the sender's Number/Name and messageBody in the tableViewCell. If same user sends the message just update the messageBody text. If different user sends the message, check if user exists in db, if not, add the user in the tableView and so on... check user for every message and likewise populate the tableView.</p>
  
  <p>Here is what I have done so far:</p>
  
  <pre><code>class ContactsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
  
  var messagesArray:[String] = [String]()
  var messageBody : [String] = [String]()
  var realm : Realm!
  var userInfo = MessageData()
  
  // viewDidLoad()    // assign tableView delegate, datasource and register the Nib.
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return messagesArray.count
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = contactsTableView.dequeueReusableCell(withIdentifier: "contactscell", for: indexPath) as! ContactsListCell
          cell.userNameLabel.text = messagesArray[indexPath.row]
          cell.userImageView.image = UIImage(named: "default-user")
          cell.messageBodyLabel.text = messageBody[indexPath.row]
          return cell
      }
  } // end of class
  
  extension ContactsViewController: XMPPStreamDelegate {
  
  func xmppStream(_ sender: XMPPStream, didReceive message: XMPPMessage) {
      let string = message.prettyXMLString()
      let xml = try! XML.parse(string!)
      let messageSenderNo = xml["message"]["data”][“phoneNumber”].text
      let senderName = xml["message"]["data”][“name”].text
      let messageText = xml["message"]["data”][“text”].text
      let messageTime = xml["message"]["data"]["time"].text
      userInfo.messageFrom = messageSenderNo
      userInfo.messageBody = messageText
      userInfo.senderName = senderName
  
      realm = try! Realm()
              try! realm.write {
                  realm.add(userInfo, update: true)     
              }
  
      let senderOfMsg:[String] = realm.objects(MessageData.self).value(forKey: "messageFrom") as! [String]!               
      let textMessage:[String] = realm.objects(MessageData.self).value(forKey: "messageBody") as! [String]!
  
      messagesArray = senderOfMsg                
      messageBody = textMessage
  }   
  }
  </code></pre>
  
  <p>This is my <strong>Message</strong> Class:</p>
  
  <pre><code>class MessageData: Object{
  
  dynamic var messageFrom:String = ""
  dynamic var messageBody:String = ""
  dynamic var messageTime:String = ""
  dynamic var messageSenderName:String = ""
  
  override static func primaryKey() -&gt; String? {
      return "messageFrom"
  }
  
  convenience init(messageFrom: String, messageBody: String, messageSenderFirstName: String, messageSenderLastName: String, message_id: String, messageTime: String){
      self.init()
      self.messageFrom = messageFrom
      self.messageBody = messageBody
      self.messageTime = messageTime
      self.messageSenderName = messageSenderName
  }
  }
  </code></pre>
  
  <p>I am using Realm for first time, so I don't know If I have done some fatal mistake. If at all the data gets saved in the realm database. For the first time tableView should be blank , but after there is data in the database, the tableView must show the contacts and messageBody. It should show previous messages if any, just like <em>WhatsApp</em> when user launches the app again. The data in the tableView should persist. Please tell me what I am doing wrong or have not done. Thank you for your time.</p>