1

Trying to read the email of the current user(user1) who registered as a "Guests" in a certain hotel. so in my tableView, there will be a list of hotels that the current user entered as a guest.

  • Hotels
    • (AutoID)
    • Name: Mariot
    • Adress: ########
    • Contact: ########
    • Email: ########
    • uniqueID: ########
    • Guests
      • (AutoID)
      • Email: user1@gmail.com
      • Contact: ########
    • (AutoID)
    • Name: Discovery
    • Adress: ########
    • Contact: ########
    • Email: ########
    • uniqueID: ########
    • Guests
      • (AutoID)
      • Email: user1@gmail.com
      • Contact: ########

I tried this code but it didnt work.

var hotelRequests : [DataSnapshot] = []

override func viewDidLoad() {
    super.viewDidLoad()

    if let email = Auth.auth().currentUser?.email {
        Database.database().reference().child("Hotels").queryOrdered(byChild: "Name").queryEqual(toValue: email).observe(.childAdded, with: { (snapshot) in
            if let hotelDictionary = snapshot.value as? [String:AnyObject] {
                if let nameOfHotel = hotelDictionary["name"] as? String {

                } else {
                    self.hotelRequests.append(snapshot)
                    self.tableView.reloadData()
                }
            }
        }
    }
}

I want to see a list in my tableView of hotels that the current user(user1) became a guest.

UPDATED CODE

var hotels : [DataSnapshot] = []
var guest : [DataSnapshot] = []

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as UITableViewCell

if (tableView.tag == 1) {
        if let email = Auth.auth().currentUser?.email {

             Database.database().reference().child("hotels").queryOrdered(byChild: "email").queryEqual(toValue: email).observeSingleEvent(of: .childAdded, with: { (snapshot) in

                let snapshot = self.hotels[indexPath.row]

                if let hotelsDictionary = snapshot.value as? [String:AnyObject] {
                    if let typeOfhotel = hotelsDictionary["typeOfHotel"] as? String {

                            cell.textLabel?.text = typeOfhotel

                    }
                }
            })
    }

}
else if (tableView.tag == 2) {

    if let email = Auth.auth().currentUser?.email {


Database.database().reference().child("guest").queryOrdered(byChild: "guest Email").queryEqual(toValue: email).observeSingleEvent(of: .childAdded, with: { (snapshot) in

            let snapshot = self.guest[indexPath.row]

            if let guestDictionary = snapshot.value as? [String:AnyObject] {
                if let aTypeOfguest = guestDictionary["TypeOfGuest"] as? String {
                     cell.textLabel?.text = aTypeOfguest
                }
            }
        })
    }

}

return cell

}

Nick
  • 104
  • 11

1 Answers1

1

Your current data structure makes it easy to find the guests for a given hotel. It does not however make it easy to find the hotels for a given guest.

The reason for this is that Firebase Database queries work on a list-model. So you can for example order/filter hotels by their name or address, but not by their guests. So in your current data model you'll need to read all data of all hotels in order to then determine the list of hotels for a user.

To allow querying the user for their hotels, add an inverted data structure:

userHotels: {
  uid1: {
    hotelId1: true,
    hotelId2: true
  },
  uid2: {
    hotelId2: true,
    hotelId3: true
  }
}

With this additional data structure it becomes trivial to load the hotels for a user.

Instead of true, you can also store some information about the hotel, so that you don't have to join that in your client-side code.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Hi @frank. I already change the structure. So now i have 2 tableViews. but how come the the 2nd tableView is not showing anything. iv'e updated the codes on top. – Nick Aug 04 '19 at 09:11