0

I am working on an app that connects students at my University. Each student will make an account with a variety of profile attributes and as a student, you are able to search other profiles using a filter within the app. So, for example, if I am a freshman interested in joining Big Brothers Big Sisters I can easily search for a "Senior" who is part of "Big Brothers Big Sisters" to ask questions about the organization.

Right now, I am working on the filter and need some help. I am using Swift 4 with a Google Firebase backend. When clicking the search button, after specifying the filter details, I am able to get the filter to work for a single attribute. For example, below I am searching for every user in the database that has a "mateType" (year in school) value of "Senior" (this will later be a variable that pulls the title of the button):

dataRef.reference(withPath: "USERS/").queryOrdered(byChild:"mateType").queryEqual(toValue: "Senior").observeSingleEvent(of: .value, with:
        { snapshot in
            var fireAccountArray: [SearchUsers] = []

            for fireAccount in snapshot.children {
                let fireAccount = SearchUsers(snapshot: fireAccount as! DataSnapshot)
                fireAccountArray.append(fireAccount)
            }

            self.users = fireAccountArray

            self.tableView.delegate = self
            self.tableView.dataSource = self
            self.tableView.reloadData()
            super.viewDidLoad()
    })

The issue I am running into is that I want to filter multiple attributes. So, given my earlier example, if I want to find a "Senior" in "Big Brothers Big Sisters", is there a way to filter both of these fields in the search? Can I add another queryEqual() method to the code?

Another question I have is about sorting my search results. I want to sort by firstName, which is an attribute in my database. I got it to sort this way when I didn't have the queryEqual() method:

        dataRef.reference(withPath: "USERS/").queryOrdered(byChild:"firstName").observeSingleEvent(of: .value, with:
        { snapshot in
            var fireAccountArray: [SearchUsers] = []

            for fireAccount in snapshot.children {
                let fireAccount = SearchUsers(snapshot: fireAccount as! DataSnapshot)
                fireAccountArray.append(fireAccount)
            }

            self.users = fireAccountArray

            self.tableView.delegate = self
            self.tableView.dataSource = self
            self.tableView.reloadData()
            super.viewDidLoad()
    })

Now that I have the queryEqual() method, I need to populate the queryOrdered() method with my child key to get it to work. Is there a way I can order by firstName, but also filter by other child keys?

Thanks in advance for the help! I am really excited about this app and look forward to your responses.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • This has been asked many many times here on SO. [Here](https://stackoverflow.com/questions/28604658/querying-data-from-firebase-using-multiple-filters) and [This one](https://stackoverflow.com/questions/45433558/how-to-apply-multiple-filter-in-firebase-query-in-swift) and also [that](https://stackoverflow.com/questions/41038689/fetch-users-data-only-if-it-matches-current-users-data/41046405#41046405). It's a good question but has been asked before. Look over the examples and see what you can come up with. Marking as duplicate. – Jay Feb 25 '18 at 13:54
  • Oh - you should break out your sorting question as a separate question; questions should be one at a time so answers can be directed to that question. There are a lot of questions about sorting as well so doing a bit of research will mostly likely provide and answer. – Jay Feb 25 '18 at 13:57

0 Answers0