我试图在扁平化数据结构和减少非规范化过程中对服务器的调用次数之间找到适当的平衡点。我的iOS应用程序有2个模型"团队"和#34;玩家"。
"players": {
"p1ID": {
"firstName": "Joe",
"lastName": "Smith",
"team": "teamID",
}
}
"teams": {
"t1ID": {
"player1": "playerID",
"player2": "playerID",
}
}
下面我将收到我当前用户的小组,然后为每位玩家再打两次电话。
let currentUserRef = rootRef.child("players/" + currentUser.uid)
// get current user's team ID
currentUserRef.child("/team").observeSingleEventOfType(.Value, withBlock: { snapshot in
let teamID = snapshot.value as! String
let teamRef = rootRef.child("teams/" + teamID)
// get current user's team data
teamRef.observeEventType(.Value, withBlock: { snapshot in
// get player 1 data
let player1ID = snapshot.value!["player1"] as! String
rootRef.child("players/" + player1ID).observeSingleEventOfType(some callback)
// get player 2 data
let player2ID = snapshot.value!["player2"] as! String
rootRef.child("players/" + player2ID).observeSingleEventOfType(some callback)
})
})
从SQL背景来看,不断对服务器进行这些嵌套调用并不常见。这是典型的吗?将玩家信息移动到下面的团队会更好吗?假设我还可以让每个玩家的团队成员信息与玩家本身嵌套。
"players": {
"p1ID": {
"firstName": "Joe",
"lastName": "Smith",
"team": "teamID",
}
}
"teams": {
"t1ID": {
"player1": {
"ID": "player1ID",
"firstName": "Joe",
"lastName": "Smith"
}
"player2": {
...
}
}
}