简体   繁体   English

带有 Firebase 身份验证用户的 Firebase 数据库数据结构

[英]Firebase Database data structure with Firebase Authentication user

I have (somewhat) large list of jokes in my Firebase Database like in the image below.我的 Firebase 数据库中有(有点)大的笑话列表,如下图所示。

当前笑话节点

I display them in a list in my Android app something like a feed.我将它们显示在我的 Android 应用程序中的列表中,类似于提要。 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).我还实现了使用 Firebase 身份验证登录的可能性,现在我想为登录用户添加选项以(不)喜欢笑话并将它们添加到收藏夹(收藏夹应该像私人书签)。 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.添加名为“userJokes”的新根节点,其子节点代表来自 Firebase 身份验证的用户 UID。 Every UID child node should have copy of every joke from "joke" node with additional booleans representing (dis)like and favorite states.每个 UID 子节点都应该有来自“笑话”节点的每个笑话的副本,并带有表示(不)喜欢和喜欢状态的附加布尔值。

    建议1

  2. Another solution is to add every user UID to a joke a user (dis)likes or adds to favorite.另一种解决方案是将每个用户 UID 添加到用户(不)喜欢或添加到收藏夹的笑话中。

    建议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?将每个笑话从“笑话”节点复制到“userJokes”节点以在提要中显示每个用户的最佳方法是什么? 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?可以将用户 UID 从 Firebase 身份验证添加到 Firebase 数据库中的数据库吗?

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 ),最好只向其添加简单值而不复制整个源数据( jokes数据),如下所示:

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).所以它将是userJokeLikeuserJokeFavorite (或类似的东西)。 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 )每个笑话数据仍然在它们的源路径中(即在jokes )并且只有它们的 id 被复制到新创建的数据路径中( userJokeLikeuserJokeFavorite

  • When you want to search for joke that user with id randomUserId likes, you should check for userJokeLike\\randomUserId .当您想搜索 id 为randomUserId用户喜欢的笑话时,您应该检查userJokeLike\\randomUserId Then from every joke id you got there, get the real data from inside source jokes path.然后从你得到的每个笑话 id 中,从源jokes路径中获取真实数据。

  • When you want to search for joke that is favorited by user with id randomUserId , basically, do the same as above.当您想搜索 id 为randomUserId的用户喜欢的笑话时,基本上,执行与上述相同的操作。

  • 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注意:我还没有检查最后的代码,希望有效:p

EDIT:编辑:

Looks like I did misunderstand :D看来我误会了: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.当您请求jokes数据时,它包括likesfavorite SO in each data you only need to check if current user's UID is exist inside likes and/or favorite .因此,在每个数据中,您只需要检查当前用户的 UID 是否存在于likes和/或favorite And the counter will be a lot easier this way.这样计数器会容易得多。

Happy coding :)快乐编码:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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