title: How to fix a UITableView class which repeat twice elements read on Firebase database and in the load the table is empty?
body: <p>I'm trying to read from firebase realtime database a series of instructions.</p>
  
  <p>I have an unknown number of information on my Vehicles node, so I just use <code>nextObject</code> method to get the number of times that the function need to iterate.</p>
  
  <p>The problem is that at the opening, my table is empty. When I click on the searchbar then my cells contents did appear. How can I solve these problem?</p>
  
  <p>Here my <code>UITableView</code> file:</p>
  
  <pre><code>import UIKit
  import FirebaseDatabase
  import Alamofire
  
  
  
  
  class Vehicles: UITableViewController,
  UISearchResultsUpdating, UISearchBarDelegate {
  
      //variables
      var model: NSMutableArray = []
      var numberOfVehicles: NSMutableArray = []
      var price: NSMutableArray = []
      var imagePathString: NSMutableArray = []
      var detailpage: NSMutableArray = []
  
  
  
      var populator: NSMutableArray = []
      var searching = false
      var matches = [Int]()
      let searchController = UISearchController(searchResultsController: nil)
  
      @IBOutlet weak var InfoTableView: UITableView!
  
      var InfoList: [String] = []
  
      override func viewDidLoad() {
          super.viewDidLoad()
          loadData()
          //this should reload but, it didn't.
          self.InfoTableView.reloadData()
          //then the searchbar that is good and don't have any problem.
          searchingField()
      }
  
  
  
      func loadData() {
          //read data from database
          let rootRef = Database.database().reference()
          let conditionalRef = rootRef.child("Vehicles")
          conditionalRef.observe(.value) {(snap: DataSnapshot) in
              // Get all the children from snapshot you got back from Firebase
              let snapshotChildren = snap.children
              // Loop over all children in Firebase
              while let child = snapshotChildren.nextObject() as? DataSnapshot {
                  // Get code node key and save it to they array
                  self.populator.add(child.key)
                  if self.populator.contains("\(child.key)") {
                      let userRef = rootRef.child("Vehicles").child("\(child.key)")
  
                      userRef.observeSingleEvent(of: .value, with: { snapshot in
                          let userDict = snapshot.value as! [String: Any]
  
                              let model1 = userDict["Model"] as! String
                              self.model.add(model1)
  
                              let detail1 = userDict["Detail"] as! String
                              self.detailpage.add(detail1)
  
                              let numberOfVehicles1 = userDict["numberOfVehicles"] as! String
                              self.numberOfVehicles.add(numberOfVehicles1)
  
                              let Price1 = userDict["Price"]  as! String
                              self.price.add(Price1)
  
                              let imageURL1 = userDict["imageURL"]  as! String
                              self.imagePathString.add(imageURL1)
                      }) //end second observeSingleEvent
  
                  }
                else {
                      let alert = UIAlertController(title: "Error", message: "No one vehicle found", preferredStyle: UIAlertController.Style.alert)
                      alert.addAction(UIAlertAction(title: "ok", style: UIAlertAction.Style.default, handler: nil))
                  self.present(alert, animated: true, completion: nil)
                  }
  
              } //end searching object in Vehicles node
  
  
  
              } //end first observeSingleEvent
  
        }//end func
  
  
      func searchingField() {
          //setup searchbar
          tableView.estimatedRowHeight = 50
          navigationController?.navigationBar.prefersLargeTitles = true
  
          searchController.searchBar.delegate = self
          searchController.searchResultsUpdater = self
          searchController.searchBar.backgroundColor = UIColor.white
          searchController.obscuresBackgroundDuringPresentation = false
          searchController.searchBar.placeholder = "Search"
          navigationItem.searchController = searchController
          definesPresentationContext = true
          let attributes = [
              NSAttributedString.Key.foregroundColor : UIColor.black,
              NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 17)
          ]
          UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
          UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "Dismiss"
          UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])
      }
  
  
  
  
  
      // MARK: Search Controller
      func updateSearchResults(for searchController: UISearchController) {
          var regArray = self.model as NSArray as! [String]
          if let searchText = searchController.searchBar.text,
              !searchText.isEmpty { 
              matches.removeAll()
  
              for index in 0..&lt;model.count {
                  if regArray[index].lowercased().contains(
                      searchText.lowercased()) {
                      matches.append(index)
                  }
              }
              searching = true
          } else {
              searching = false
          }
          tableView.reloadData()
      }
  
      func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
          searching = false
          tableView.reloadData()
      }
  
  
  
      // MARK: - Table view data source
  
      override func numberOfSections(in tableView: UITableView) -&gt; Int {
          return searching ? matches.count : model.count
      }
  
      override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
  
          return searching ? matches.count : model.count
      }
  
  
      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
  
          let cellIdentifier = "TableCell"
  
          let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! Vehicles_cell
  
  
  
          let row = indexPath.row
          var regArray = self.model as NSArray as! [String]
          cell.Label.text = searching ? regArray[matches[row]] : model[row] as! String
          cell.Subtitle?.text = "N. Vehicles: \(self.numberOfVehicles[indexPath.row]) - Price: \(self.price[indexPath.row])$"
  
  
          Alamofire.request("\(self.imagePathString[indexPath.row])").response { response in
              guard let image = UIImage(data:response.data!) else {
                  // Handle error
                  return
              }
              let imageData = image.jpegData(compressionQuality: 1.0)
              cell.Image.contentMode = .scaleAspectFit
              cell.Image.image = UIImage(data : imageData!)
          }
  
          return cell
      }
  
  
  
      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
          if segue.identifier == "ShowrentDetails" {
  
              let myIndexPath = self.tableView.indexPathForSelectedRow!
  
              //save detail1 in UserDefault
              let SVDetail = self.detailpage[myIndexPath.row]
              let SVDetaildefaults = UserDefaults.standard
              SVDetaildefaults.set(SVDetail, forKey: "sv_detail")
              SVDetaildefaults.synchronize()
              _ = segue.destination
                  as! Vehicles_Detail
          }
      }
  
      //IMPOSTA LE DIMENSIONI DELLE CELLE
      override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
          switch indexPath.row {
          default:
              return 100
          }
      }
  
  
  }
  </code></pre>
  
  <p>I expect the table show on the opening all the data from database, while actually  and not repeat unless I click on the searchbar. And the table shouldn't be repeated twice.</p>
  
  <hr>
  
  <p><strong>Edit (solution to duplicates)</strong></p>
  
  <p>This is so embarrassing. The answer to this problem is simple</p>
  
  <p>In <code>numberOfSections</code> function, I used address.count instead to use 1 Section. So, what I saw were not duplicate cells, but new block sections of <code>model.count</code></p>