If I fan out my data like this in Firebase (for a social network):
post
postId
postText: This is a post
postedBy: bob
comment
commentId
commentText: This is a comment
postedBy: fred
like
likeId
postedBy: george
Then when I want to build a tableview of posts I have to embed a LOT of observers. For example:
ref.child("post").observeSingleEventOfType(.Value, withBlock: {
ref.child(userId).observeSingleEventOfType(.Value, withBlock: {
})
})
That is needed to get the post data and the user data if you want to pos the user's name and whatever else. Then if you want to do number of likes you have to embed it inside, and if you want to do some preview comments like Instagram does, you have to embed another... and if you have more creative things to add it goes on and on like so:
ref.child("post").observeSingleEventOfType(.Value, withBlock: {
ref.child(userId).observeSingleEventOfType(.Value, withBlock: {
ref.child("comments").observeSingleEventOfType(.Value, withBlock: {
ref.child(commentUserId).observeSingleEventOfType(.Value, withBlock: {
ref.child("likes").observeSingleEventOfType(.Value, withBlock: {
// create post object here and append to array
})
})
})
})
})
So anyway it gets pretty crazy if you want to get the people who liked, and have more stuff to add. My question is, is this okay to do in Firebase and does this make the fan-out data methods still worth using? I feel like it would slow it down so much that we would be better off just putting all the stuff underneath the post object in Firebase. I did read that observers aren't very expensive but it didn't say much about nesting them.