I'm trying to find the right balance between a flattened data structure and reducing the # of calls to the server in the process of denormalization. My iOS app has 2 models "Teams" and "Players".
"players": {
"p1ID": {
"firstName": "Joe",
"lastName": "Smith",
"team": "teamID",
}
}
"teams": {
"t1ID": {
"player1": "playerID",
"player2": "playerID",
}
}
Below I am getting my current user's team, and then making 2 more calls for each player.
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)
})
})
Coming from an SQL background it doesn't feel normal to be constantly making these nested calls to the server. Is this typical? Would it be better to move the player info into the team like below? Suppose I could also have each player's team member info nested with the player itself.
"players": {
"p1ID": {
"firstName": "Joe",
"lastName": "Smith",
"team": "teamID",
}
}
"teams": {
"t1ID": {
"player1": {
"ID": "player1ID",
"firstName": "Joe",
"lastName": "Smith"
}
"player2": {
...
}
}
}