简体   繁体   中英

Firebase Database data structure with Firebase Authentication user

I have (somewhat) large list of jokes in my Firebase Database like in the image below.

当前笑话节点

I display them in a list in my Android app something like a feed. I also implemented possibility to log in with Firebase Authentication and now I want to add options for logged users to (dis)like jokes and add them to favorites (favorites are supposed to be like private bookmarks). I'm wandering how I could structure my data and I have two proposals:

  1. Add new root node called "userJokes" with child nodes representing user UID from Firebase Authentication. Every UID child node should have copy of every joke from "joke" node with additional booleans representing (dis)like and favorite states.

    建议1

  2. Another solution is to add every user UID to a joke a user (dis)likes or adds to favorite.

    建议2

First solution is logical, but how could I count number of likes and dislikes if I structure data this way? And what is the best way to copy every joke from "joke" node to "userJokes" node for every user to be showed in the feed? Second is impractical since while retrieving jokes, I will get info about every user that has (dis)liked or added to favorites and this is not what I need. Which solution is better? Is there any other? Is it OK to add user UID from Firebase Authentication to database in Firebase Database?

I think the first one is more accepted, although it needs some tweak :)

First note : if you create data only to be used as relational (like userJokes ), it's better to just add simple value to it without copying entire source data ( jokes data), like this:

userJokes: {
    randomUserId: {
        randomJokeId:true,
        anotherRandomJokeId:true
    }
    awesomeUser: {
        randomJokeId:true
    }
}

Second note : if you want to implement two functionality (like and favorite), I think you should make it as different data. So it would be userJokeLike and userJokeFavorite (or something like that). And the structure for each of them should be same as I mentioned in first note.


In conclusion :

  • Every joke data is still in their source path (ie inside jokes ) and ONLY their id is copied into newly created data path ( userJokeLike and userJokeFavorite )

  • When you want to search for joke that user with id randomUserId likes, you should check for userJokeLike\\randomUserId . Then from every joke id you got there, get the real data from inside source jokes path.

  • When you want to search for joke that is favorited by user with id randomUserId , basically, do the same as above.

  • When you want to count likes and favorite of each joke, just use something like this:

     FirebaseDatabase.getInstance().getReference("userJokeLike") .orderByChild().equalsTo("randomJokeId") .addListenerForSingleValueEvent(new ValueEventListener() { ... onDataChange(DataSnapshot dataSnapshot) { int jokeCount = dataSnapshot.getChildrenCount(); } });

And there you go, hope this helps.

Note: I haven't check the last code, hope that work :p

EDIT:

Looks like I did misunderstand :D

The solution above is what I think is best for the sake of structure itself. But if we need something simple and fast, it is different for each case/situation. I think that the best solution if you want to get jokes with likes and favorites included (no need to create another request) then your structure should look like this:

jokes: {
    randomJokeId: {
        // joke data here
        likes:{
            randomUserId:true,
            anotherUserId:true
        },
        favorites:{
            randomUserId:true
        }
    }
}

It includes likes and favorite when you request jokes data. SO in each data you only need to check if current user's UID is exist inside likes and/or favorite . And the counter will be a lot easier this way.

Happy coding :)

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