More efficient way to retrieve Firebase Data?

前端 未结 1 1256
别跟我提以往
别跟我提以往 2020-12-17 04:36

I have a hierarchical set of data that I want to retrieve information from Firebase. Below is how my data looks:\"enter

相关标签:
1条回答
  • 2020-12-17 04:44

    One of the golden rules of Firebase is to only nest your data when you always want to retrieve all of it. The reason for this rule is that Firebase always returns a complete node. You cannot partially retrieve some of the data in that node and not other data.

    The Firebase guide on structuring data, says this about it:

    Because we can nest data up to 32 levels deep, it's tempting to think that this should be the default structure. However, when we fetch data at a location in our database, we also retrieve all of its child nodes. Therefore, in practice, it's best to keep things as flat as possible, just as one would structure SQL tables.

    You should really read that entire section of the docs, since it contains some pretty good examples.

    In your case, you'll need to modify your data structure to separate the event attendees from the event metadata:

    events
      -JFSDFHdsf89498432
        eventCreator: "Stephen"
        eventCreatorId: 1764137
      -JOeDFJHFDSHJ14312
        eventCreator: "puf"
        eventCreatorId: 892312
    event_attendees
      -JFSDFHdsf89498432
        -JSAJKAS75478
          name: "Johnny Appleseed"
        -JSAJKAS75412
          name: "use1871869"
      -JOeDFJHFDSHJ14312
        -JaAasdhj1382
          name: "Frank van Puffelen"
        -Jo1asd138921
          name: "use1871869"
    

    This way, you can retrieve the event metadata without retrieving the attendees and vice versa.

    0 讨论(0)
提交回复
热议问题