Tableview is showing data two times

The tableview is showing me data two times when I add a data.
Do you have any idea about why this is happening?
My codes are below.

import UIKit
import Firebase

class OyuncularVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

var items = [ItemModel]()

@IBOutlet weak var tblView: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    retrieveItems()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.items.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FeedTableViewCell
    
    let row = self.items[indexPath.row]
    
    cell.lblTitle.text = row.title
    
    return cell
}

/* Retriev Items */

func retrieveItems() {
    DataService.dataService.ITEM_REF.observe(.value, with: { (snapshot: DataSnapshot?) in
        
        if let snapshots = snapshot?.children.allObjects as? [DataSnapshot] {
            self.items.removeAll()
            print(snapshots.count)
            for snap in snapshots {
                if let postDic = snap.value as? Dictionary<String, AnyObject> {
                    let itemModel = ItemModel(key: snap.key, dictionary: postDic)
                    
                    self.items.append(itemModel)
                    print(itemModel)
                    self.items.insert(itemModel, at: 0)
                }
            }
            
            self.tblView.reloadData()
        }
        
    })
    
}

You both append and insert the itemModel object to your list of data. So it's in there twice.

1 Like

Excellent!! Thanks so much