Home Retrieve data from firebase database to class in tableView
Reply: 0

Retrieve data from firebase database to class in tableView

user3364
1#
user3364 Published in June 18, 2018, 3:34 am

I'm trying to get data from my users from my database. Me need to display all data in tableView on current authorized user.

Now i have this struct in firebase:

{
  "users" : {
    "fZjVIhQrqvcQYIa0CjfmDNnaUao2" : {
      "email" : "email@email.email",
      "historyBooking" : {
        "-L5FGrgRULvn_E4GV9ht" : {
          "bookingDate" : "13.02.2018",
          "bookingTime" : "C 02:00 до 3:00 (итого 1 час.)",
          "email" : "",
          "name" : "",
          "phonenumber" : "",
          "studioHallAddress" : "AddressOne",
          "studioHallName" : "HallOne",
          "surename" : "",
          "totalSum" : "1500 руб."
        },
        "-L5FO_GQSLixyRBtjZVt" : {
          "bookingDate" : "14.02.2018",
          "bookingTime" : "C 11:00 до 12:00 (итого 1 час.)",
          "email" : "",
          "name" : "",
          "phonenumber" : "",
          "studioHallAddress" : "AddressTwo",
          "studioHallName" : "HallTwo",
          "surename" : "",
          "totalSum" : "1900 руб."
        }
      },
      "uid" : "fZjVIhQrqvcQYIa0CjfmDNnaUao2",
      "username" : "admin"
    }
  }
}

And i have this code:

class HistoryBookingViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    var historyBooking: [BookingHall] = []

    var userHistoryRef: DatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()

        userHistoryRef = Database.database().reference()

        loadHistoryBooking()

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return historyBooking.count

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "historyCell", for: indexPath) as! HistoryBookingTableCell

//        cell.studioHallName.text = historyBooking[indexPath.row].studioHallName

        return cell

    }

    func loadHistoryBooking() {

        if let userID = Auth.auth().currentUser?.uid {

            userHistoryRef.child("users").child(userID).child("historyBooking").observe(.value, with: { (snapshot) in

                let value = snapshot.value as! NSDictionary

                print("value - \(value)")

                self.tableView.reloadData()

            })
        }
    }
}

In console i see all data, but how i can apply all data from firebase to my class?

My class is here:

class BookingHall {

    var ref: DatabaseReference!
    var studioHallName: String = ""
    var studioHallAddress: String = ""
    var bookingDate: String = ""
    var bookingTime: String = ""
    var totalSum: String = ""
    var name: String = ""
    var surename: String = ""
    var email: String = ""
    var phonenumber: String = ""

    init(snapshot: DataSnapshot) {

        ref = snapshot.ref

        let value = snapshot.value as! NSDictionary

    }
}

I know I'm missing code in my class and in the loadHistoryBooking() function, please tell me what to add in my function and in my class.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.318948 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO