title: Table view shows duplicate data in iOS Swift using Firestore?
body: <p>Here i am using firestore database. I am fetching user table data into table view cell. In table view listing all user with follow button option. If user runs for first time table view does not duplicate data, when user closed app and came back again, goes to people suggestion tab to list all user and its all users are duplicated. </p>
  
  <p>Here is the screen shot: <a href="https://i.stack.imgur.com/kVsj7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kVsj7.png" alt="enter image description here"></a></p>
  
  <pre><code> import UIKit
  
   class peopleViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  
  @IBOutlet weak var tableView: UITableView!
  
  var users: [Users] = []
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      title = "People"
      loadUsers()
  
  }
  
  
  
  func loadUsers() {
  
  
      API.User.observeUser { (user) in
          self.isFollowing(userId: user.id!, completed: { (value) in
              user.isFollowing = value
  
              self.users.append(user)
              self.tableView.reloadData()
  
          })
      }
  
  }
  
  
  func isFollowing(userId: String, completed: @escaping (Bool) -&gt; Void) {
      API.Follow.isFollowing(userId: userId, completed: completed)
  }
  
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if segue.identifier == "ProfileSegue" {
          let profileVC = segue.destination as! UserViewController
          let userId = sender  as! String
          profileVC.userId = userId
          profileVC.delegate = self as? UserViewControllerDelegate
      }
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
  
      return users.count
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "PeopleTableViewCell", for: indexPath) as! peopleTableViewCell
  
  
      let user = users[indexPath.row]
      print("letuser::::\(user)")
      cell.user = user
      cell.delegate = self
      return cell
  }
  
  
  
  
  
   }
    extension peopleViewController: PeopleTableViewCellDelegate {
    func goToProfileUserVC(userId: String) {
      performSegue(withIdentifier: "ProfileSegue", sender: userId)
  
  
   }
  }
  </code></pre>
  
  <p>Here is the code how fetch data from firestore :</p>
  
  <pre><code>     func observeUser(completion: @escaping (Users) -&gt; Void) {
  
      db.collection("users")
          .getDocuments() { (querySnapshot, err) in
              if let err = err {
                  print("Error getting documents: \(err)")
              } else {
  
                  self.userList.removeAll()
  
                  var user : Users?
                  for document in querySnapshot!.documents {
  
                       user = Users.transformUser(postDictionary: document.data(), key: document.documentID)
      //                        self.userList.append(user!)
                      completion(user!)
                  }
  
      //                    completion(user!)
  
              }
      }
  }
  </code></pre>