0

Hi Fellow Firebase Devs!

Earlier this year i got hired into a startup and we got going developing our app in xamarin and we decided to use firebase as our backend. A decision i in some ways regret as its not really as .net/xamarin friendly as for example azure.

However after some workarounds and lessons learnt during this year we got a stable firebase integration into our app. Release is in a couple of weeks so now we are doing some final touches to our backend which leads into my question.

At the moment our Realtime Database is structured in this way:

-posts
   -id
      -id
      -title
      -content
      -etc

 -user-profiles
    -id
       -id
       -name
       -age
       -etc

So i am basically storing the Id for each item twice. First i though saving the id as field for each object would be practical because when i deserialize the json into .net objects i have a ID property in each of my models to simplify my business logic.

But none of the firebase examples use this logic nor any 3rd party tutorials.

Is there any cleaner database structure i could use? Cheers and thanks in advance!

2 Answers2

0

Well, both posts and user-profiles are supposed to be collections - but the way you're implementing, RTDB will deal with it as objects. Personally, I use the same approach as yours, and that's because of how the RTDB works - you can send a path directly to it, and fetch the desired data. The other alternative would be storing like an array, but you'll lose the control over these guys - not necessarily, but it's not so intuitive as storing it inside a big object, and fetching it by its id when necessary.

If you're worried about duplicating your data, you can implement a way to handle this when reading/writing in your "front-end" code.

Guilherme Matuella
  • 2,193
  • 1
  • 17
  • 32
0

So I am basically storing the Id for each item twice.

I'm not a fan of duplicating the key of an object in the object itself. But that is a personal preference and not a hard rule. I just find that it clutters my code more than it helps.

I usually prefer either keeping the snapshot around (so that I can get the key from there), or injecting the key into the data object right after I deserialize the DataSnapshot into such an data object. For an example of this (in Android), see Is there a way to store Key in class which I cast from Firebase object?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807