How to populate tableView with data from childByAutoID











up vote
2
down vote

favorite












I have a FirebaseStructure like so:



 "users" : {    "OpeHS4UhH6YWcK4aPHJWumueCQP2" : {
"Orders" : {
"Date" : {
"-LQYDWAKlzTrlTtO1Qiz" : 1541410526186,
"-LQYspEghK3KE27MlFNE" : 1541421618601,
"-LQsILjpNqKwLl9XBcQm" : 1541764115618
},
"Order" : {
"-LQYDWAKlzTrlTtO1Qiy" : "1 Tomato, 3 Orange, 2 Banana",
"-LQYspEfZaJIt-PaAvYa" : "1 Banana, 9 Apple, 2 Tomato",
"-LQsILjpNqKwLl9XBcQl" : "1 Apple"
}
}
}


I want to display the order history in a tableView

How can I populate the tableView with the "Order" without the childByAutoID?

I have tried this, but it comes up empty:



class OrderHistoryTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var orderDateHistoryArray = [Double]()
var orderItemsHistoryArray = [String]()

@IBOutlet var tableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

getOrderHistory()
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return orderItemsHistoryArray.count
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "OrderHistoryCell") as! OrderHistoryTableViewCell
cell.orderHistoryItemsLabel.text = orderItemsHistoryArray[indexPath.row]
return cell
}

func getOrderHistory() {
//Get userinfo from database
let uid = Auth.auth().currentUser!.uid
let orderDateHistoryRef = Database.database().reference().child("users/(uid)/Orders/")
orderDateHistoryRef.observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let orderItem = value?["Order"] as? String ?? ""
self.orderItemsHistoryArray += [orderItem]

print(snapshot)
print(orderItem)

self.tableView.reloadData()
// ...
}) { (error) in
print(error.localizedDescription)
}
}
}









share|improve this question




























    up vote
    2
    down vote

    favorite












    I have a FirebaseStructure like so:



     "users" : {    "OpeHS4UhH6YWcK4aPHJWumueCQP2" : {
    "Orders" : {
    "Date" : {
    "-LQYDWAKlzTrlTtO1Qiz" : 1541410526186,
    "-LQYspEghK3KE27MlFNE" : 1541421618601,
    "-LQsILjpNqKwLl9XBcQm" : 1541764115618
    },
    "Order" : {
    "-LQYDWAKlzTrlTtO1Qiy" : "1 Tomato, 3 Orange, 2 Banana",
    "-LQYspEfZaJIt-PaAvYa" : "1 Banana, 9 Apple, 2 Tomato",
    "-LQsILjpNqKwLl9XBcQl" : "1 Apple"
    }
    }
    }


    I want to display the order history in a tableView

    How can I populate the tableView with the "Order" without the childByAutoID?

    I have tried this, but it comes up empty:



    class OrderHistoryTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var orderDateHistoryArray = [Double]()
    var orderItemsHistoryArray = [String]()

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
    super.viewDidLoad()

    getOrderHistory()
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return orderItemsHistoryArray.count
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "OrderHistoryCell") as! OrderHistoryTableViewCell
    cell.orderHistoryItemsLabel.text = orderItemsHistoryArray[indexPath.row]
    return cell
    }

    func getOrderHistory() {
    //Get userinfo from database
    let uid = Auth.auth().currentUser!.uid
    let orderDateHistoryRef = Database.database().reference().child("users/(uid)/Orders/")
    orderDateHistoryRef.observeSingleEvent(of: .value, with: { (snapshot) in
    // Get user value
    let value = snapshot.value as? NSDictionary
    let orderItem = value?["Order"] as? String ?? ""
    self.orderItemsHistoryArray += [orderItem]

    print(snapshot)
    print(orderItem)

    self.tableView.reloadData()
    // ...
    }) { (error) in
    print(error.localizedDescription)
    }
    }
    }









    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have a FirebaseStructure like so:



       "users" : {    "OpeHS4UhH6YWcK4aPHJWumueCQP2" : {
      "Orders" : {
      "Date" : {
      "-LQYDWAKlzTrlTtO1Qiz" : 1541410526186,
      "-LQYspEghK3KE27MlFNE" : 1541421618601,
      "-LQsILjpNqKwLl9XBcQm" : 1541764115618
      },
      "Order" : {
      "-LQYDWAKlzTrlTtO1Qiy" : "1 Tomato, 3 Orange, 2 Banana",
      "-LQYspEfZaJIt-PaAvYa" : "1 Banana, 9 Apple, 2 Tomato",
      "-LQsILjpNqKwLl9XBcQl" : "1 Apple"
      }
      }
      }


      I want to display the order history in a tableView

      How can I populate the tableView with the "Order" without the childByAutoID?

      I have tried this, but it comes up empty:



      class OrderHistoryTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

      var orderDateHistoryArray = [Double]()
      var orderItemsHistoryArray = [String]()

      @IBOutlet var tableView: UITableView!

      override func viewDidLoad() {
      super.viewDidLoad()

      getOrderHistory()
      }


      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return orderItemsHistoryArray.count
      }

      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
      return 100
      }

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "OrderHistoryCell") as! OrderHistoryTableViewCell
      cell.orderHistoryItemsLabel.text = orderItemsHistoryArray[indexPath.row]
      return cell
      }

      func getOrderHistory() {
      //Get userinfo from database
      let uid = Auth.auth().currentUser!.uid
      let orderDateHistoryRef = Database.database().reference().child("users/(uid)/Orders/")
      orderDateHistoryRef.observeSingleEvent(of: .value, with: { (snapshot) in
      // Get user value
      let value = snapshot.value as? NSDictionary
      let orderItem = value?["Order"] as? String ?? ""
      self.orderItemsHistoryArray += [orderItem]

      print(snapshot)
      print(orderItem)

      self.tableView.reloadData()
      // ...
      }) { (error) in
      print(error.localizedDescription)
      }
      }
      }









      share|improve this question















      I have a FirebaseStructure like so:



       "users" : {    "OpeHS4UhH6YWcK4aPHJWumueCQP2" : {
      "Orders" : {
      "Date" : {
      "-LQYDWAKlzTrlTtO1Qiz" : 1541410526186,
      "-LQYspEghK3KE27MlFNE" : 1541421618601,
      "-LQsILjpNqKwLl9XBcQm" : 1541764115618
      },
      "Order" : {
      "-LQYDWAKlzTrlTtO1Qiy" : "1 Tomato, 3 Orange, 2 Banana",
      "-LQYspEfZaJIt-PaAvYa" : "1 Banana, 9 Apple, 2 Tomato",
      "-LQsILjpNqKwLl9XBcQl" : "1 Apple"
      }
      }
      }


      I want to display the order history in a tableView

      How can I populate the tableView with the "Order" without the childByAutoID?

      I have tried this, but it comes up empty:



      class OrderHistoryTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

      var orderDateHistoryArray = [Double]()
      var orderItemsHistoryArray = [String]()

      @IBOutlet var tableView: UITableView!

      override func viewDidLoad() {
      super.viewDidLoad()

      getOrderHistory()
      }


      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return orderItemsHistoryArray.count
      }

      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
      return 100
      }

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "OrderHistoryCell") as! OrderHistoryTableViewCell
      cell.orderHistoryItemsLabel.text = orderItemsHistoryArray[indexPath.row]
      return cell
      }

      func getOrderHistory() {
      //Get userinfo from database
      let uid = Auth.auth().currentUser!.uid
      let orderDateHistoryRef = Database.database().reference().child("users/(uid)/Orders/")
      orderDateHistoryRef.observeSingleEvent(of: .value, with: { (snapshot) in
      // Get user value
      let value = snapshot.value as? NSDictionary
      let orderItem = value?["Order"] as? String ?? ""
      self.orderItemsHistoryArray += [orderItem]

      print(snapshot)
      print(orderItem)

      self.tableView.reloadData()
      // ...
      }) { (error) in
      print(error.localizedDescription)
      }
      }
      }






      swift firebase firebase-realtime-database






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 14:06









      Frank van Puffelen

      221k25362387




      221k25362387










      asked Nov 9 at 13:05









      Neccer

      646




      646
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Order is a Dictionary not a String



          if  let orderItem = value?["Order"] as? [String:String] {
          self.orderItemsHistoryArray += Array(orderItem.values)
          }





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226285%2fhow-to-populate-tableview-with-data-from-childbyautoid%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            Order is a Dictionary not a String



            if  let orderItem = value?["Order"] as? [String:String] {
            self.orderItemsHistoryArray += Array(orderItem.values)
            }





            share|improve this answer

























              up vote
              1
              down vote



              accepted










              Order is a Dictionary not a String



              if  let orderItem = value?["Order"] as? [String:String] {
              self.orderItemsHistoryArray += Array(orderItem.values)
              }





              share|improve this answer























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                Order is a Dictionary not a String



                if  let orderItem = value?["Order"] as? [String:String] {
                self.orderItemsHistoryArray += Array(orderItem.values)
                }





                share|improve this answer












                Order is a Dictionary not a String



                if  let orderItem = value?["Order"] as? [String:String] {
                self.orderItemsHistoryArray += Array(orderItem.values)
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 9 at 13:12









                Sh_Khan

                34.9k51125




                34.9k51125






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226285%2fhow-to-populate-tableview-with-data-from-childbyautoid%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Schultheiß

                    Android Play Services Check

                    Where to put API Key in Google Cloud Vision for PHP