-1

Due to Parse announcement, I just switched to Firebase. I have a 15 years background on 'old school' databases architecture and I have to say, I am having a hard time to understand if I am doing right...

Here is the idea : I am creating an iOS app showing all the good places in town. Few categories such as 'Bars', 'Restaurants', ....

The point is I, for sure, want to list each company from each category in a TableViewList, but I also want to be able to count how many company I have in a category to show the result on the main page (like Bars : 36, but I didn't get the whole list yet... just the list of categories...)

Here is a picture of what I am trying to create from now on.

firebase_screenshot

Just a bit confused and need a hand to start properly I guess...

Btw, This is my first message on StackOverFlow after spending few years getting some answers to my problems. So thank you all for the great ideas and support!

Vincent
  • 13
  • 5
  • Welcome to StackOverflow. And welcome to NoSQL. :-) Your question is incredibly broad, which makes it difficult to answer here on StackOverflow. It's likely that it will be closed for that reason. But a few things to get you started: Firebase does not have a built-in way to get the count of child nodes. When you load a node, you get everything under it too. See this question for one way to keep a count: http://stackoverflow.com/questions/15148803/in-firebase-is-there-a-way-to-get-the-number-of-children-of-a-node-without-load or my answer here: http://stackoverflow.com/a/26498693 – Frank van Puffelen Feb 07 '16 at 05:23
  • For a primer on typical NoSQL data modeling patterns, I recommend this article: https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/. Also read the post from Alex Memering here: https://groups.google.com/forum/#!topic/firebase-talk/1p4O4Pc2w6k and this answer from David East: http://stackoverflow.com/questions/35170616/firebase-and-select-distinct/35177398#35177398 – Frank van Puffelen Feb 07 '16 at 05:35
  • Thanks for the answers Frank. on second thought you're right the question is broad, but it didn't look like that from here... I am too into it. Should be why ;) Thanks anyway I do appreciate. – Vincent Feb 07 '16 at 05:57

1 Answers1

1

While this is a broad question, and there are 100 different ways to structure your data here's one possible starting point:

cool_places
   random_place_id_0
     name: "Jaycins"
     type: random_type_0
   random_place_id_1
     name: "Tony's Pizza Palace"
     type: random_type_0
   random_place_id_2
     name: "Jake's Dive Bar"
     type: random_type_1
types
   random_type_0
     type: "Restaurant"
   random_type_1
     type: "Bar"
counts
   random_type_0: 2
   random_type_1: 1

First thing to do is to separate your data from the node names, so for example, the random_place_id_x is a auto generated Firebase id.

With this structure, we can easily grab a list of types (Restaurant, Bar), can get all of the cool_places of a certain type, and with one read we know number of certain types of restaurants from the counts node.

The only gotcha is ensuring when restaurant is added or removed to update the counter accordingly.

You could also structure your data to keep all the data about Restaurants in a node, Bars in another node

cool_places
  random_node_0
     type: random_type_0
     count: 2
     list:
       random_place_id_0
         name: "Jaycins"
       random_place_id_1
         name: "Tony's Pizza Palace"

types
  random_type_0: "Restaurant"
  random_type_1: "Bar"

While at first glance, this looks pretty snappy, to get all of the counts for the different types, it will require a query that may return a lot of data or multiple queries.

If you are interested in a count for only one type at a time, say when the user selects Show Me Restaurants, then you're good to go.

But, denormalization is always the best way to go - the flatter the better, so suggestion 1 is the winner.

edit

Another last minute thought

types
   random_type_0
     type: "Restaurant"
     count: 2
   random_type_1
     type: "Bar"
     count: 1
Jay
  • 34,438
  • 18
  • 52
  • 81
  • Thanks Jay! It makes me understand a little bit more... Actually my question was more a confirmation request. I had the same idea as your first example. But the concept is a little bit hard to get for me... but I'll do it ;0) Thanks for that good example. – Vincent Feb 08 '16 at 07:26
  • You talked about the auto_generated firebase iD. I have a simple question about it. If I got everything according Firebase documentation, the auto generated ID can be created only through the client side during the save process right? My categories (BARS, RESTAURANTS, etc) are statics so I have to create them in Firebase, but I'll assign an auto generated ID by the device each time someone adds a new place. Sounds ok? – Vincent Feb 08 '16 at 07:31
  • That's right on point. Even though they are static, always let their parent be an auto generated id, you never know what other stuff you may want to add as a child; type: Restaurant, count: 2, description: A restaurant is a eatery where 51% of the revenue is food. You can use whatever scheme you want, but disassociating the actual textual data from node names is a really good investment in your data structure. Fortunately the Firebase auto_id makes it a snap. – Jay Feb 08 '16 at 19:08