0

I'm building a specific database (picture below) and I want to display the results in the labels. The first label should show the number of all customers - it's simple, but the second label should show the number of all the child's clients, for example: if the customer Ben has one child, and Tom has one child - label shows 2 (number of child's clients).

enter image description here

Is possible to do this?

My code:

let userID = Auth.auth().currentUser!.uid 
ref.observeSingleEvent(of: .value, with: { snapshot in 
  if let allServices = snapshot.childSnapshot(forPath: "usersDatabase/(userID)/Customers").value { 
    if snapshot.childrenCount == 0 { 
      self.servicesLabel.text = "0" 
    } else { 
      self.servicesLabel.text = (allServices as AnyObject).count.description 
    } 
  } 
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Sounds possible. Can you show the code of where you got stuck while implementing this? – Frank van Puffelen May 13 '18 at 22:32
  • @FrankvanPuffelen let userID = Auth.auth().currentUser!.uid ref.observeSingleEvent(of: .value, with: { snapshot in if let allServices = snapshot.childSnapshot(forPath: "usersDatabase/\(userID)/Customers").value { if snapshot.childrenCount == 0 { self.servicesLabel.text = "0" } else { self.servicesLabel.text = (allServices as AnyObject).count.description } } – Christopher Lowiec May 14 '18 at 13:45

1 Answers1

0

The key here is that since the usersDatabase node is read by .value, iterating over each child node and treating it as a snapshot will give you the counts.

let usersDatabaseRef = Database.database().reference().child("usersDatabase")
usersDatabaseRef.observe(.value, with: { snapshot in
    print("there are \(snapshot.childrenCount) users")
    var totalCustomerCount = 0
    for child in snapshot.children {
        let childSnap = child as! DataSnapshot
        let childrenRef = childSnap.childSnapshot(forPath: "Customers")
        totalCustomerCount += Int(childrenRef.childrenCount)
        print("user \(childSnap.key) has \(childrenRef.childrenCount) customers")
    }
    print("... and there are \(totalCustomerCount) total customers")
})

asssuming there are three users in the usersDatabase node, will print the following

there are 3 users
user uid_0 has 2 customers //this is the 7U node
user uid_1 has 1 customers
user uid_2 has 3 customers
... and there are 6 total customers

Edit: added code to count and display total customers across all child nodes.

Jay
  • 34,438
  • 18
  • 52
  • 81
  • let userID = Auth.auth().currentUser!.uid let usersDatabaseRef = Database.database().reference().child("usersDatabase").child(userID).child("Customers") usersDatabaseRef.observe(.value, with: { snapshot in print("there are \(snapshot.childrenCount) users") self.clientsLabel.text = snapshot.childrenCount.description for child in snapshot.children { let childSnap = child as! DataSnapshot let childrenRef = childSnap.childrenCount print("user \(childSnap.key) has \(childrenRef) customers") – Christopher Lowiec May 14 '18 at 19:38
  • after little modification this code works! But is one problem - how to show in label count of customers (in your example: 6)? – Christopher Lowiec May 14 '18 at 19:40
  • @KrzysztofŁowiec not sure I understand the question - you are asking about a label - which label? Like a static text label in your UI? The count of customers is different for each user (I assume) so where do you want to see that count? – Jay May 14 '18 at 22:22
  • I have other static label in UI and I want to show in it the sum of count all my users. Is it possible? – Christopher Lowiec May 15 '18 at 13:56
  • @KrzysztofŁowiec The code in my answer shows the count of all users - where it says *there are 3 users*. Is that the count you are asking about? – Jay May 16 '18 at 18:23
  • That's perfect but i ask about count of customer's child - in my example I have path userDatabase/userID/Customers - Ben Smith contain 1 child and Tom Cruise 1 child - i want to show sum of their child (2) – Christopher Lowiec May 16 '18 at 18:45
  • @KrzysztofŁowiec Ok, I understand. Edited the answer to print the total number of customers for all users. – Jay May 16 '18 at 18:53
  • After delete "childSnapshot(forPath: "Customers")" from "let childrenRef = childSnap.childSnapshot(forPath: "Customers")" your code works perfect! Thanks man so much! – Christopher Lowiec May 16 '18 at 20:07
  • Can you look for my question? https://stackoverflow.com/questions/50383693/fetch-all-children-from-database?noredirect=1#comment87787490_50383693 – Christopher Lowiec May 18 '18 at 07:42
  • can you look for my other question? https://stackoverflow.com/questions/50553655/database-count-of-values – Christopher Lowiec May 27 '18 at 18:01