Home > Blockchain >  How to search items in firebase through code
How to search items in firebase through code

Time:02-20

I want to make medstore app. In it i made one option category for "all items". So no problem there, because i will fetch all items from firebase through recycler view.

I am inserting items in firebase through admin app.

Now the problem is that i want to categorised all items like

Fitness, ayurvedic, Homeopathic

So i have two options to do so .

  1. I will make another node for in firebase for it .like Seprate node of ayurvedic ,seprate for fitness.

    And all items have many properties like

    Name, price , description, stock etc
    

    I guess it's not a good way beacuse it will consume more space unnessarily more than once.

    Because there is already one node for " all items " .

    So one item will cosume two space one in "all items" and other in "ayurvedic".

    Or

  2. I will just put the " key " of items in specific node .not a full detail.

    And recycler view goes to main position(at node "all items" ) of items by seeing its key and will fetch all data like price from it.

    But how can i do so ?

And can anyone will explain me please which one is the best way to do?

CodePudding user response:

You can add a "category" field in each item. So that if you add a new item in the "all items" node, the item has its own unique category.

In the client side, you can add a filter functionality. If the user filters by fitness, then query all items with category equal to fitness.

Something like

rf.orderByChild("category").equalTo("Fitness");

Now you can get all the items and you can also get specific categories without consuming extra storage.

Here is a guide on querying real-time database

  • Related