1

I've seen this answered quite a lot for Database, but never Firestore. I'm having some difficulty with doing this. In particular, I can't seem to be able to order fields in my collection "users" by "username". I want to be able to display a user's username on a Cell in my Table View. I've tried using NSDictionary to append users' "username" to Dictionary. As you can probably tell, I'm very new to coding. So, any answers would be massively appreciated. This is my latest attempt. I know it's very wrong, I just don't know how to correct it:

 var userArray = [NSDictionary]()
var filteredUsers = [NSDictionary?]()

let db = Firestore.firestore()

override func viewDidLoad() {
    super.viewDidLoad()

    searchController.searchResultsUpdater = self
    definesPresentationContext = true
    tableView.tableHeaderView = searchController.searchBar


    searchController.searchResultsUpdater = self
    definesPresentationContext = true
    tableView.tableHeaderView = searchController.searchBar

    let userID = Auth.auth().currentUser?.uid
    db.collection("users").order(by: "username").getDocuments()
        { (snapshot, error) in
            self.userArray.append(snapshot.value as? Dictionary)

                self.findUsersTableView.insertRows(at: [IndexPath(row:self.userArray.count-1, section: 0)], with: UITableView.RowAnimation.automatic)
            if error != nil {
                print(error?.localizedDescription)
            }
    }

    }
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
GabrielB
  • 143
  • 8
  • Where exactly are you having issues ? Can you try to print the snapshot.value before adding it to the dictionary to see if it's being fetched correctly and in the right order ? This will allow you to see if the issue is within the orderby usage or afterwards in the addition to the dict. But please try to explain in more detail where you are stuck and what errors/results you are getting vs what you expect. – Waelmas Jun 15 '20 at 11:06
  • Hey - thanks for your comment. I think printing the snapshot value is my main problem. I’ve noticed that on YouTube, you get the value with “.value”. Whenever I try this, however, it never recognises and autocompleted to just “.value”. Instead, I can either do “.value(forKey: ...)” or “.value(forKeyPath...)”. I’ve tried using these, but I always get a “not key value coding compliant” for the value I enter into the .value. If I ignore autocomplete and simply enter “.value” without any parentheses after, I get an error saying “it is ambiguous without more context”. I hope this clarifies! – GabrielB Jun 15 '20 at 12:32
  • Here's another SO thread addressing what you are trying to achieve. https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase It seems like you might be running the orderby query wrongly compared to the data structure you have in your DB. Please take a look at the thread in addition to this as well where both explain a very similar goal and how to achieve it. https://stackoverflow.com/questions/33976380/how-to-do-order-by-query-using-firebase Also the official docs: https://firebase.google.com/docs/firestore/query-data/order-limit-data#swift – Waelmas Jun 16 '20 at 12:01

0 Answers0