One-To-Many Relationship Firebase Realtime Database (with miliions of commented embedded)

Issue

I am new to Firebase Realtime Database, coming from a relational database background. I have designed a post structure with many comments, but I don’t know how to load them. A record is given below from that collection:

{
   _id: ObjectId("63173b1411db4b2f8e32f3cf"),
   title: "How to load data in mongoDB",
   comments: [
        {
            userId: ObjectId("63173b1411db4b2f8e32fcfb"),
            comment: "Thanks",
        },
        {
            userId: ObjectId("63173b1411db4b2f8e323fcb"),
            comment: "Nice Post",
        },
        ...
   ]
}

Now when there are hundreds of millions of comments, then how should I load them, because if I load them at once it takes a lot of time and space. What can be the optimal solution for this?

Solution

I have designed a post structure with many comments, but I don’t know how to load them.

The best option that you have, is to load the comments progressively. This practice is called pagination. This means that you need to load the data progressively in smaller chunks. This topic was already covered a lot of times before, and this question already has a lot of solutions for the Realtime Database:

Now when there are hundreds of millions of comments, then how should I load them, because if I load them at once it takes a lot of time and space. What can be the optimal solution for this?

There is no chance you can load such an enormous amount of data at once. Most likely you’ll end up getting an OutOfMemoryError. Besides that, I don’t think that anybody will be interested in reading hundreds of millions of comments.

However, if you consider at some point in time trying using Cloud Firestore, I think that this answer will help:

If you want to go further and try using Jetpack Compose, then I think that this resource will help:

How to implement pagination in Firestore using Jetpack Compose?

Answered By – Alex Mamo

Answer Checked By – Clifford M. (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *