简体   繁体   中英

Cannot figure out Firebase Database structure

I've completely redone this database a few times now and I can't figure out the puzzle of it all.

The basic idea at the moment is a user makes a post and people can submit replies to it. On View 1, "initiated posts" are viewed, on View 2 you see all of the posts you've replied, on view 3 you can click into a specific post and see all the responses, and on view 4 you can see a detail page for any specific reply.

I have this all working, but I've got two major issues after implementing a friend system and delete system.

I'll make an example post to help explain:

View 1:

A bunch of topics from your friends list

Talk about cats Talk about cars Talk about Swift

View 2:

Post you've joined in about dogs in the past post you've joined in about cars in the past the post about cats you just responded to

View 3 after clicking on the cats post:

User 1 : Cats suck User 2 : Cats are cool

View 4 after clicking on user a user:

Detailed view of that post and that user

Bob Bobs picture "Cats suck"

I'm needing users to be able to have customized views of view 3 and 4 because I want them to be able to delete certain posts and individual posts.

So instead of there being a User 1 and User 2 under cats, if someone really didn't like what someone said or it was just a troll submission from User 1 they could delete it.

The issue is that I'm currently just pulling all of the replies from underneath the post's "replies" child..I could temporarily delete a post from the view via scratching it from the array, but when the page is reloaded it'd load up all of the posts again, so obviously the user needs their own place they're keeping track of the individual replies made to a main post.

For example I need some way for each user to have a reference to "Cats are cool" and "cats suck"

On View 2 those can easily just be deleted by deleting the reference to that post in their user info which is only created when they respond to it in the first place, but beyond that I don't know how to get the individual replies to the users.

Very roughly because at this point I've reworked it all so many times:

Users
   09283490823
      RepliedToPosts
          ABCDEFG : true
      FriendsPosts
           friendUID : postID
           friendUID: postID
           // reworked all of this, dunno how I want this structured anymore but this part will be ok

  posts:
      ABCDEFG
         *replies*

so I'm observing the friendsPosts on View1, observing the repliedToPosts on View 2 which I can delete, and then on view3 I just going into posts and viewing all the replies, and then further on view 4 viewing one at an individual level.

I had everything working and fine and dandy until I forgot right up at the end that I'm wanting to be able to delete individual posts on View3 and I haven't found a way to implement that.

Any ideas? I can't just have individual replies sent to all "participants" because people can join in late...no idea really.

What you need to do is "fanout" your data as much as possible. That means duplicating the data (in your case the responeses) to multiple locations in the db so it will be easier to get them. You can have 1 node called "postsReplies" with post IDs and replies and another node called "repliesByCategory" where you will see something like.

Cats: {
 some_cat_reply_Id: {
   // the reply data
}
}

The fanout happens on the client side - when the user posts a reply it gets posted to all the nodes.

Here are a couple of links that might help you learn more about "fanout" data:

https://medium.com/@gilg/from-sql-to-firebase-how-to-structure-the-db-for-a-social-network-app-95b0aa5664c0?source=linkShare-af537a57f12f-1469861757

https://firebase.googleblog.com/2015/10/client-side-fan-out-for-data-consistency_73.html?m=1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM