Rafael Lima
Rafael Lima

Reputation: 3535

Firebase on android how to return only the node without children

This might be an odd question. However, I am running out of the free quota of my firebase database and before start paying for it, I want to optimize some of my firebase queries.

I was observing the firebase console where we can manage the database and I noticed that it only shows us the nodes on each level. To see the children we must click on the + button (next to the node name) or at the node itself.

I believe this behaviour is designed to avoid requesting the whole database at once what would lead to a huge traffic and resource consumption. I also believe that firebase console was built on firebase public API, so it should be possible for us to implement the same behaviour.

My question is considering this following database:

{  
   "root":{  
      "node1":{  
         "childA":"a",
         "childB":"b",
         "childC":"c"
      },
      "node2":{  
         "childA":"2a",
         "childB":"2b",
         "childC":"2c"
      },
      "node3":{  
         "childA":"3a",
         "childB":"3b",
         "childC":"3c"
      },
      "node4":{  
         "childA":"4a",
         "childB":"4b",
         "childC":"4c"
      },
      "lululu":{  
         "childA":"1a",
         "childB":"2b",
         "childC":"3c"
      },
      "node1214":{  
         "childA":"1a",
         "childB":"1b",
         "childC":"1c"
      },
      "node10":{  
         "childA":"a",
         "childB":"b",
         "childC":"c"
      }
   }
}

How can I get the list of nodes under root without their content (as a list for example like ["node1", "node2", "node3"...] or as a proper Map but without the child data (to reduce bandwidth usage)?

Upvotes: 0

Views: 282

Answers (2)

Reaz Murshed
Reaz Murshed

Reputation: 24211

In my opinion, the data structure should be as flat as possible to avoid nested data. You might consider restructuring your data as follows. You might consider taking a look at the best practices of storing and organizing your data in Firebase.

{  
   "root": {  
       "node1":"1",
       "node2":"2",
       "node3":"3"
   },

    "nodes" : {  
       "1": {  
           "childA":"a",
           "childB":"b",
           "childC":"c"
       },
       "2":{  
           "childA":"2a",
           "childB":"2b",
           "childC":"2c"
       },
       "3":{  
           "childA":"3a",
           "childB":"3b",
           "childC":"3c"
       }
    }
}

When you get the item, you get the children under the item as well. So avoid nesting your data. You might consider checking the answer here as well.

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317372

This is not possible with any of the mobile SDKs. It's only possible with the REST API using the shallow parameter.

Upvotes: 1

Related Questions