Reputation: 932
Note: I am a newbie in flutter, Android and Firebase.
So, I have a logo Maker App, the assets(PNG Images) such as the face, hat etc. are saved in the firestore
Database. I have a total of 71 documents in my database currently.
Currently, I use cached_network_image plugin. With this plugin, the image will be read-only one time for a single user. Since from next time image will be retrieved from cache Storage. But the retrieval of the title of the image is still a problem.
Still, for a single user, I get around 100 reads! This is the schema of my database.
Kinda Obvious different collection for different parts, and one document for one image.
Using StreamBuilder to retrieve data,
Is there any way I can reduce the number of read request by either code or changing database schema or something else?
Upvotes: 9
Views: 1745
Reputation: 57
One thing I would suggest is moving your stream declaration outside of your build method, because every time your widget rebuilds that stream will be called again.
@override
void initState() {
mainStream = FirebaseFirestore.instance
.collection('users')
.doc('images')
.snapshots();
super.initState();
}
Then use mainStream inside your StreamBuilder. That way your stream will be initialized on startup and won't be called on rebuild every time.
Also just keep in mind that viewing your database on Firebase also counts as reads every time you refresh the page and view any docs, so your reads might not actually be that high when just using the app :)
EDIT: I would suggest making these changes then just playing around with your app like a user would, then going to your Firebase Project and viewing the reads for the past 60 minutes, that should tell you better how many actual reads you have
Upvotes: 1
Reputation: 17523
One option you might want to consider is combining things into fewer documents.
I'm not exactly sure how you're planning on using this data from the database, but it seems like your main use case is "read in everything from the faces collection and display it in a list". If that's the case (and these records are relatively small) do you need to put every face in a separate document? If you're not going to paginating this list, or searching for and retrieving subsets of this collection, you could just create a JSON object containing all of your face data and put that into a single document.
Do that for each of your other records, and you've gone from about 71 documents to 7. That's a 10x savings!
Upvotes: 1