I am doing a practice app on an instagram like app on firebase and swift. The question I have is that I am finding myself nesting my firebase "observeSingleEventOfType" to obtain all the data I need such that I can put it in my "Post Class". I was wondering if there is a better way of doing it as it seems like it could take up alot of time just to load one post? (I am grabing one info, wait, then grab the next, wait, then grab the next,wait,grab the next... and put everything as one Post Item at the end of the chain)
Upon initial load, I need the location, top comments, whether or not the user liked the post, whether the user followed the post or not. Currently what I am doing is
->Create a geo query, grab the geo information. Inside the completion block, I have a observeSingleEventOfType to look for information from my posts node and store as dictionary. Inside the completion block of this, I create another observeSingleEventOfType call to get the top comments, then inside this completionblock, I create another observeSingleEventOfType to look for whether or not the user is following the post. Then inside this, I have all the data to store inside my Post class that looks like
init(postKey: String, distance: Double, topComments: [Comment], liked: Bool, followingPost: Bool, dictionary: Dictionary<String, AnyObject>) {}
Here is my Json structure.
{
"comments" : {
"postKEY123" : {
"CommentKEY123" : {
"comment" : "aa"
}
}
},
"follow" : {
"userId123" : {
"postKEY123" : true
}
},
"posts" : {
"postKEY123" : {
"commentCount" : 1,
"postDescription" : "Aa",
"likeCount" : 0,
"userId" : "userId123"
}
},
"users" : {
"userId123" : {
"email" : "user1@mail.com"
"liked" : {
"postKey123": true
}
},
}
"location" : {
"postKey123" : {
".priority" : "9q9hrjj7cd",
"g" : "9q9hrjj7cd",
"l" : [ 37.33769226, -122.02885785 ]
},
},
}
The reason that I have done it this way is so that the Json data is not nested. As an possible alternative method, would it be possible to fetch all the data for the post async in background and return it when all the info for that post is ready? I didnt think I could do that as you wouldnt be able to guarantee what and when the item comes back. This is why I grabbed the data one by one inside completion block of one another?
Thanks,