1

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": {
         ...
       }
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Making many calls to the Firebase Database server is not a problem. See http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786. You'll want to track your listeners though, so that you don't end up with multiple listeners per player (and disconnect listeners from players you're no longer showing). – Frank van Puffelen Jul 12 '16 at 18:03
  • Downvoter: please leave a comment when you downvote. As far as I can tell this question shows effort and is clear. – Frank van Puffelen Jul 12 '16 at 18:05
  • I am not the downvoter, but I can understand the thinking. The question asks for what is best, without specifying criteria to be used. Because of that, answers are necessarily opinions, and not objective results. Opinions are going to draw downvotes – Feldur Jul 12 '16 at 20:20
  • Thanks for the help @FrankvanPuffelen and for the feedback @Feldur! – Maddison Brusman Jul 14 '16 at 16:20
  • @Feldur wouldn't that be better as a Close vote? Might be more of a discussion for meta I guess. – Frank van Puffelen Jul 14 '16 at 16:43

0 Answers0