Gil Gil - 1 year ago 66
iOS Question

Firebase - proper way to structure the DB

I have an iOS app that is like a social network for music.
In the app, users share "posts" about specific music "tracks".
I want to know the best way to structure the DB in Firebase, considering that each "post" object references a single "track" object.

Also, when a user submits a new post, I need to check if a track already exists by querying the artist + song title - if the track does not exist, add a new track. If the track exists get the "track_id" to reference in the "post" object.

Jay Jay
Answer Source

The challenge here is performing an 'and' query in Firebase as well, that doesn't exist. So you have mush two pieces of data together to then do that query. Here's a structure

   artist_0: Pink Floyd
   artist_1: Billy Thorpe
   artist_2: Led Zeppelin

   track_id_0: Stairway To Heaven
   track_id_1: Children Of The Sun
   track_id_2: Comfortably Numb

   artist_0_track_id_2: true
   artist_1_track_id_1: true
   artist_2_track_id_0: true

      artist_track: artist_1_track_id_1
      post: Billy was one of the most creative musicians of modern times.
      artist_track: artist_0_track_id_2
      post: The Floyd is the best band evah.

With this structure, if you know the artist and the track name, you can concatenate them and do a simple query in the artists_tracks node for .equalToValue(true) to see if it exists.

The posts in the posts node tie back to those specific artists and tracks.

In some cases you can glue your data together to perform and searches without the extra nodes... like this

  artist_track: Billy_Thorpe_Children_Of_The_Sun

However, because of the spaces in the names and the varying width of the text it won't work. So that leads to ensuring you include enough digits in the data to handle however many songs and artists so the length stays consistent.

   artist_00000_track_id_00002: true

Now you can have 50,000 artists and 50,000 tracks.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download