Firebase加入多个节点对象

时间:2017-06-13 23:24:17

标签: ios swift firebase firebase-realtime-database

我在尝试在Firebase中构建数据以及检索它时遇到问题,以便满足此用例:

我有'用户'我有' Groups'

{
Users:
    LpWgezRkC6EWS0sjXEWxhFl2: {
        userName: 'John Doe'
        etc...
    }
},
Groups: 
     12345: {
          PropertiesID: 78765,                     
          GroupName: 'MyPool',
          GroupOwner: 'LpWgezRkC6EWS0sjXEWxhFl2' 
          GroupPassword: 'jkhdfjkhfdkjhdfkjhfsae4euhh4FDDC'
          etc...
     }
}

任何用户都可以加入任何群组。

我将这个用户的配对(关系)存储在" UserInGroups"节点

{
    UsersInGroups: {
        DUmwewIfzAbfWZN4NjS8mhX82: {  <- UserId
           12345: true                <- GroupId
        },
        LpWgezRkC6EWS0sjXEWxhFl2: {    
           12345: true                
        }
    }
}

最后,每个群组都有一个&#39;属性&#39;控制组的特定/行为的节点。

{
  Properties:
     78765: {
          PropertyName: 'Custom',
          dateCreated:  '20170601',
          etc...
     },
     76421: {
          PropertyName: 'Admin',
          dateCreated:  '20170602',
          etc...
     }
}

总而言之,我的应用程序有一个Dashboard视图,它在UICollectionView中显示所有组/属性信息。它应显示如下信息:

  • GroupName
  • GroupOwner
  • 基于&#39;群组中的属性ID&#39;节点

    • PropertiesName

    • 属性creationDate

我将如何进入&#34;加入节点&#34;这样我就可以检索组信息以及属性信息;

基于登录应用程序的用户是否属于该组(因此在UsersInGroups节点中有一个条目)?!

  

更新

关于数据结构的后续问题......

可以接受三(3)个连接来获取我希望在UIColloection视图中显示的所有信息。

这样的事情:

let ref = firebase.database().ref("UsersInGroups")
ref.child((Auth.auth().currentUser?.uid)!).queryEqual(toValue: true).observe(.value, with: { snapshot in

    let groupsRef = firebase.database().ref("Groups")
    groupsRef.child(snapshot.key).observe(.value, with: { snap in

        let propertiesRef = firebase.database().ref("Properties")
        ref.child(snap.key).queryEqual(toValue: true).observe(.value, with: { gamesnapshot in

              //Get all the info from firebase

        })
    })
 })

1 个答案:

答案 0 :(得分:0)

在您下载“群组”节点的所有地方,您是否需要该特定群组的属性?如果这样做,您可以在“Groups”中创建一个新节点,将其称为属性,并列出其中的所有属性。