I am creating a rating system for my App. I have two collections.
Ratings and Items.
Every time a user rates an item, It goes inside the Ratings.
(e.g rating: 1)
What I want to do is every time someone rates an item, a cloud function will trigger that will get all the ratings and put it in the Items.
(e.g total_ratings: 400)
I've read something that I need to use Sharded Counter to have it right because there is a limit in writing in one document.
Increment counter with Cloud Functions for Firebase
But I've got no knowledge in node.js or how this Sharded Counter works. I can't understand how it works.
Can someone please help me how to do this? Thank you very much.
Related
I have a collection in my Firestore Database with posts. Each post has some comments as a document in the collection comments in the main post document (screenshot)
In my Android app when I click on the post item I go to PostDetail screen and there I download comments.
I want to increment the value of timesViewed for each comment which has been downloaded and displayed.
Is it possible to do that? I want to do that in the most efficient way. I don't want to increment every single comment manually by sending a request timesViewed++.
I want to increment the value of timesViewed for each comment which has been downloaded and displayed.
If you're looking for something that does that automatically, please note that there is nothing built-in.
Is it possible to do that?
Yes, but you should implement your own mechanism.
I don't want to increment every single comment manually by sending a request timesViewed++.
Unfortunately, there is no other way. However, it is very easy to implement. You can use FieldValue.increment(1).
Unfortunately, such a feature does not exist in the RealtimeDatabase firebase and firestore to automatically increase a field.
But you can create a counter and retrieve the counter every time onResponse () or onComplete and increase it by 1, which is not recommended for 2 reasons.
Generates extra traffic, especially if the number of users is large
It is not reliable because someone can intentionally increase it
But if you do not mind a lot of traffic and you insist on doing so, do not forget to set rules in the firebase console to control the incremental frequency.
I am developing a Flutter quiz app for learning purposes using Cloud Firestore. The app is working fine, but the problem is that same user can read and play quiz multiple number of times in a single day. I want to restrict users to play quizzes only once per day. My research is not helping me out, so I decided to seek help here. How can I impose restrictions on the number of times the quiz played by users per day? Any help will be appreciated a lot.
if I interpreted your problem correctly then this will work for you...
when a user enters the game you should set the variable in shared preference like {'isJoined' : 'true' , 'joinedTime': 'dd-mm-yyyy'}
now in this case you can have multiple logic I will suggest you if your time is fixed then you can compare the current time with last joined time.
I want to restrict users to play the quiz only once per day.
The simplest solution I can think of is to create a new field in each User object called "readyToPlay". This field should have the initial value of "true". As soon as the user finishes the first game, change the value of the field to "false". Then simply restrict the possibility to play the game according to this field. So you have to check at the beginning of each game, against this newly added field.
To reset the value of this field to "true", every single day, I recommend you write a function in Cloud Functions for Firebase and call it once a day using Cloud Scheduler, as explained in my answer from the following post:
Is it not possible to have a code in the background who will be called every 24h?
yes shared preference will help you here is the example.
I am working on an android e-commerce app and recently I'm having little doubts on some concepts.
Should I include my functions in the client side apk or on the online servers (like Google functions, Amazon lambda)?
For example an user adds an item in his cart
Should I include my code in the client side apk like:
Get the item's price and multiply it with the quantity and then add to the total price of cart.
Or should I just send the data from the client side apk like:
Send the unique ID and quantity to the specific function in online functions
And then put the multiplying and stuff... in the online functions.
Where should I include these calculations?
I think the important thing here is how you're managing state. When the user adds something to their cart, does the app tell the server they've done that? Do they have a cart on the server, with a list of what that user has added so far? Or does it all happen on the user's device, and the server doesn't know anything about the cart until they go to check out?
If the cart is managed on the server, I'd say it's better to let that handle all of the state, and let it tell the device what to display. If the user adds 2 items, tell the server what they've added, and the server can say "your cart contains this" and include the price in that information. It gives you more control, because your server is the source of truth - you're not relying on the device to get it right (which sounds silly but a lot of weird things can happen)
Whatever you do though, don't let the user's device tell the server how much the items cost! If it's calculating the total for the user then fine, so long as it's for display purposes. But when it comes to calculating how much the items cost at checkout, you do that yourself, server-side - take item IDs and counts from the user, nothing else. If they fake a request that says 50 gold watches for $1, you'll be in big trouble!
That might be obvious but I had to put it out there just in case!
It depends on where you're calling that function from. If you're calling that function from somewhere in your front end code, you should include it in your client. If you're calling the function from somewhere in one of your Lambda or Google functions, then you should put it there.
The concept that you might want to learn about is called scope. Wherever a function is in scope, some thing else can call it. You might consider looking up the rules for scope in your specific programming language.
I know this question a lot here, But I don`t know about this. This is about chat app.
When message get stored into the firebase database. It would become bigger and bigger as time goes by, I want to delete it. and when should I delete this? I just want to left just only last 10 data. It means if I out the app and again go in, It appears only last 10 sentence and I know about the function limitToFirst and limitToLast (but this is not the delete thing.)
If I pay firebase server, you know, if database's data is large It will be more expensive. but I just want to leave just last 10 sentences at least.
when they did come back then they can see the last 10sentence and want to delete except for this. how do I do this?
I saw the answer using the date, but I don`t want that. Is that only answer? If I must do that when I get to delete them? When do I invoke the delete function?
I know how to do that almost, but when? If in the app, there are so many friends and I open the chat screen to chat my friend. That time should I delete them using remove function? How am I saving this for server payment?
I don`t want cost a lot. and I want them to be clear, not dirty in my Firebase database console. so want to delete them. Which is the I have to do? Which time is the best time that I should delete them? When open it ? or when close it ? or when users stop the my app.
You can achieve this in a very simple way. You can use the getChildrenCount() method on the node in which you hold the messages to see the exact number of messages. If you are using as an identifier the random key generated by the push() method, it's very easy to delete the extra messages. Because the records are by default order by date, you can easily query your database using limitToLast(limit) method and than delete the messages like this:
yourRef.child("messages").child(messageId).removeValue();
Another way to achieve this is to use Cloud Functions for Firebase.
Hope it helps.
you can use firebase functions to delete old data when new ones added to the database that is the best time, you can keep the last 100 messages or less.
My app allows users to save a "guess" about something to a database on parse, but I want to cut off these guesses at a certain time, say for example 5:00 PM CST, and also prevent multiple submissions from the same person. Any advice on how to go about this?
You can create a beforeSave function in Cloud Code and reject any saves that are outside of your timespan.
https://www.parse.com/docs/js/guide#cloud-code-beforesave-triggers