Théo
Théo

Reputation: 33

How to check and delete duplicates values in TableView?

I have a database on Firebase and a tableview. I have a list of brands, models, and year for motorcycles and I want to retrieve the list of brands on the tableview.

The problem is the DB has duplicates values. There is more than one motorcycle from Suzuki, there is more one models of SV 650, etc.

How can I check duplicates values, put it in a new array, and retrieve it in the tableview?

This is my TableViewController file:

import UIKit
import FirebaseAuth
import FirebaseDatabase

class SelectionMarqueViewController: UITableViewController {
    var posts = [Post]()

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        loadMarques()
    }

    func loadMarques() {
        var ref : DatabaseReference?
        ref = Database.database(url: "https://myride-test.firebaseio.com/").reference()
        ref?.observe(.childAdded, with: { (snapshot) in
            if let dict = snapshot.value as? [String: Any] {
                let MarqueText = dict["Marque"] as! String
                let post = Post(MarqueText: MarqueText)
                self.posts.append(post)
                print(self.posts)
                self.tableView.reloadData()
            }
        })
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath)
        cell.textLabel?.text = posts[indexPath.row].Marque
        return cell
    }
}

And this one is the file with the Post func:

import Foundation

class Post {
    var Marque: String

    init(MarqueText: String) {
        Marque = MarqueText
    }
}

Here my Firebase Database:

https://ibb.co/X5fwyqK

Actually the tableview shows the complete list of brands in the DB, and so, many times the same brands.

On the DB and code: "Marque" correspond to the brand.

Upvotes: 0

Views: 361

Answers (2)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You can implement Hashable

class Post : Hashable {
    var marque: String

    init(marqueText: String) {
        marque = marqueText
    }
    // Equatable for contains
    static func == (lhs:Post,rhs:Post) -> Bool {
      return lhs.marque == rhs.marque
    }
   // Hashable for Set
   var hashValue:Int {
    return marque.hashValue
   }

}

and use

if let dict = snapshot.value as? [String: Any] {
   let MarqueText = dict["Marque"] as! String
   let post = Post(MarqueText: MarqueText)
   self.posts.append(post)
   self.posts = Array(Set(self.posts))
   print(self.posts)
   self.tableView.reloadData()
 }

Or simply

let marqueText = dict["Marque"] as! String 
if !self.posts.map { $0.marqueText}.contains(marqueText) {
   let post = Post(marqueText:marqueText)
   self.posts.append(post)
   self.tableView.reloadData()
}

Upvotes: 1

ngbaanh
ngbaanh

Reputation: 535

Check and append if the marque is not available in the datasource of the tableview.

func appendMarqueAndReloadIfNeeded(_ marque: String) {
  if self.posts.map({ $0.Marque }).contains(marque) {
    // Do nothing
  } else {
    self.posts.append(Post(MarqueText: marque))
    self.tableView.reloadData()
  }
}

Then you call it inside observe:

///....
if let dict = snapshot.value as? [String: Any] {
  let MarqueText = dict["Marque"] as! String
  self.appendMarqueAndReloadIfNeeded(MarqueText)
}
///....

Upvotes: 0

Related Questions