0

In my data structure I a Master elements that contain children Detail elements (one-to-many), something like that:

/myapp/masters/1234/details/5678
/myapp/masters/1234/details/3456
/myapp/masters/6789/details/9876

Now when I display a list of masters in a table view in my ios app, I would like to display the number of children under each master element:

  • 1234 (2)
  • 6789 (1)

What is the best way to do that efficiently with Firebase? Should I keep a counter inside the element stored at the master level and update it transactionally every time a new detail is appended to it? Other strategy?

Sebastien
  • 3,583
  • 4
  • 43
  • 82
  • Child node count has been covered quite a bit already. I'll find one and link. A bigger concern with your data structure is that Firebase's SDKs will always retrieve complete nodes, so you will be retrieving all children whenever you retrieve a master (in which case counting them client-side should be trivial). You might want to read this section Firebase's documentation on structuring your data: https://www.firebase.com/docs/web/guide/structuring-data.html#section-denormalizing-data – Frank van Puffelen Apr 17 '15 at 17:48
  • possible duplicate of [In Firebase, is there a way to get the number of children of a node without loading all the node data?](http://stackoverflow.com/questions/15148803/in-firebase-is-there-a-way-to-get-the-number-of-children-of-a-node-without-load) – Frank van Puffelen Apr 17 '15 at 17:49

1 Answers1

1

FBDataSnapshot has a childrenCount property used like this:

[fbRef observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {

        NSInteger childCount = snapshot.childrenCount;
}

However, there's a question about what your data looks like in firebase.

Is this the structure?

myapp
   masters
      1234
        details
           5678
           3456
      6789
        details
           9876

if it does then you can use the same code above and iterate over the snapshot to get counts of sub nodes (depending on the amount of data there may be other better methods). If your FB data looks different, it will depend on the structure.

Jay
  • 34,438
  • 18
  • 52
  • 81