'How to check if a tableview contains certain text?

I have an app that allows users to save different quotes. The data gets saved in a user account using Firebase. If a user wants to see what quotes they saved, they can do so in the app as well. They simply click a button, which goes to a separate view controller and displays all saved quotes in a table view. The issue I'm having is that the same quote can be saved multiple times. So, I would like to be able to have the app check the content of the tableview and, if a quote is already saved in the table, change the save button to a saved button. Any thoughts on how to go about this?

This is what I use to save to Firebase:

import UIKit
import FirebaseDatabase
import FirebaseAuth

class QuotesViewController: UIViewController {

var ref: DatabaseReference?


override func viewDidLoad() {
    super.viewDidLoad()
    configureItems()
    ref = Database.database().reference()
    
@IBAction func saveButton(_ sender: UIButton) {
  guard let user = Auth.auth().currentUser?.uid else { return }
    ref!.child("users").child(Auth.auth().currentUser!.uid).child("Quotes").childByAutoId().setValue(quotesLabel.text!)

}
}
    

To retrieve data:

import UIKit
import FirebaseDatabase
import FirebaseAuth

class SavedQuotesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var ref: DatabaseReference?
var databaseHandle: DatabaseHandle?
var postData = [String]()


@IBOutlet weak var tableView: UITableView!

@IBOutlet weak var textLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
    
    ref = Database.database().reference()
    
    
    
    databaseHandle = ref?.child("users").child(Auth.auth().currentUser!.uid).child("Quotes").observe(.childAdded, with: { (snapshot) in
        let post = snapshot.value as? String
        
        if let actualPost = post {
        self.postData.append(actualPost)
            self.tableView.reloadData()
        }
})
        
}

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return postData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell")
        cell?.textLabel?.text = postData[indexPath.row]
        cell?.textLabel?.numberOfLines = 0

        
        return cell!
        
    }

}


Solution 1:[1]

As jnpdx said in their comment, you are thinking about this wrong. The table view is a View object. It doesn't store data, it presents it to the user and provides an interface for the user to interact with that data.

You want a model of some kind to store your data. Your Firebase database might be that model, or you might load your records into a model object in memory. I assume you've already made those decisions. (I haven't used Firebase, so I don't know if it's fast enough to serve up data for a table view or not. I suspect not. In order to support smooth scrolling, you want to be able to serve up cells as fast as possible.)

You need a way to tell if your quote is unique. As Claude suggested in his comment, you might want to set up your quote string in Firebase to be hashed/indexed. If you do that testing for uniqueness will be really fast. When the user enters a new quote, you could query the database with the quote string (or it's hash) and see if a record already exists with that hash. If it does, you could update the UI to tell the user that the quote already exists.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Duncan C