Firebase – 构build数据库的正确方法

我有一个像音乐社交networking的iOS应用程序。 在应用程序中,用户分享有关特定音乐“曲目”的“post”。 我想知道在Firebase中构build数据库的最佳方式,因为每个“后”对象都引用单个“轨道”对象。

另外,当用户提交新post时,我需要通过查询艺术家+歌曲标题来检查曲目是否已经存在 – 如果曲目不存在,则添加新的曲目。 如果曲目存在,请在“发布”对象中引用“track_id”。

这里面临的挑战是在Firebase中执行“和”查询,这个查询不存在。 所以你有两块数据在一起,然后做这个查询。 这是一个结构

artists artist_0: Pink Floyd artist_1: Billy Thorpe artist_2: Led Zeppelin tracks track_id_0: Stairway To Heaven track_id_1: Children Of The Sun track_id_2: Comfortably Numb artists_tracks artist_0_track_id_2: true artist_1_track_id_1: true artist_2_track_id_0: true posts post_id_0 artist_track: artist_1_track_id_1 post: Billy was one of the most creative musicians of modern times. post_id_1 artist_track: artist_0_track_id_2 post: The Floyd is the best band evah. 

使用这种结构,如果您知道艺术家和曲目名称,则可以将它们连接起来,并在artists_tracks节点中为.equalToValue(true)做一个简单的查询以查看它是否存在。

post节点中的post与特定的艺术家和曲目相关联。

在某些情况下,您可以将您的数据粘贴在一起执行和search,而无需额外的节点…就像这样

 stuff artist_track: Billy_Thorpe_Children_Of_The_Sun 

但是,由于名称中的空格和文本的宽度不同,它将不起作用。 这样就可以确保在数据中包含足够的数字来处理许多歌曲和艺术家,因此长度保持一致。

 artists_tracks artist_00000_track_id_00002: true 

现在你可以有五万名艺术家和五万首歌曲。

在这种情况下,当您执行轨道searchfunction并search跟踪轨道的用户时,您将遇到一些麻烦。 所以一般来说,您需要在您的客户端应用程序中至less加载一个表。 我希望这可以帮助你以后的麻烦。