我有一个类似于音乐社交网络的iOS应用。
在该应用程序中,用户共享有关特定音乐“曲目”的“帖子”。
考虑到每个“发布”对象都引用一个“跟踪”对象,我想知道在Firebase中构建数据库的最佳方法。
另外,当用户提交新帖子时,我需要通过查询艺术家+歌曲标题来检查曲目是否已经存在-如果该曲目不存在,请添加新曲目。如果轨道存在,请获取“ track_id”以在“ post”对象中进行引用。
最佳答案
这里的挑战也是在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.
通过这种结构,如果您知道艺术家和曲目名称,则可以将它们连接起来,并在artist_tracks节点中对.equalToValue(true)进行简单查询以查看其是否存在。
“帖子”节点中的帖子与这些特定的艺术家和曲目相关。
在某些情况下,您可以将数据粘合在一起以执行和搜索,而无需额外的节点……
stuff
artist_track: Billy_Thorpe_Children_Of_The_Sun
但是,由于名称中的空格和文本宽度的变化,因此无法使用。这样可以确保您在数据中包含足够的数字以处理许多歌曲和艺术家,从而使长度保持一致。
artists_tracks
artist_00000_track_id_00002: true
现在您可以拥有50,000位艺术家和50,000条曲目。