Update multiple values of different nodes every second in firebase - android

I am creating an app in which users earn points. Here is my data structure:
How can I update all points of different nodes by 5 every second and repeat this process?

You can use the cloud script function of Firebase as implementing this logic on client side will drain the battery as you need to update the value after every one second.
On Cloud script you can use crown job to implement this logic and it will not put any load on the application too.

Related

How to count how many times document was downloaded in Firebase?

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.

How to automatically delete firestore documents which are created before N days?

I have data structure like this:
Employees (Collection) > {EmployeeID} (Documents) > Chat (Collection) > {ChatId} (Documents).
In chat collection each document having 3 fields. 1. senderName, 2. sendTimestamp, 3. messageText.
I want to delete chats which are older than 7 days (from today).
I think it might be possible through cloud function but I am really basic user and don't know much about cloud functions. Please note that I don't want to make it automatically (cron job). I will do it manually on daily basis or whenever I wish.
I really searched a lot for this but its really hard. Please help me.
A big part of this task involves querying a sub collection. You can read more about this idea here: Firestore query subcollections
There are basically two options at the time of writing this:
Query the entire top level collection (Employees) something like db.collection('Employees').get(). Then you would have to loop through each employ object querying for their sub collection (Chat) based on their date range. Firestore query by date range for more reading on querying by a date in firestore. This could result in a large amount of reads depending on the number of Employee documents, but is the "easiest" approach in terms of not having to make changes to your data models/application.
Restructure your data to make the sub collection Chat a top level collection. Then you can do a query on this top level collection by the date. Less reads, but may not be as feasible depending on if this app is in production/willingness to make code changes.
A Function would definitely be able to accomplish this task either way you decide to approach it. One thing to note is that a Function executes using the Admin SDK, meaning it can basically ignore security rules set up on your Firestore.

How to remove child nodes in firebase after due date? [duplicate]

This question already has answers here:
Delete firebase data older than 2 hours
(5 answers)
Closed 4 years ago.
I'm making a meeting app where user can create a group of people who are part of that meeting , also date and time of the meeting has to be set when group is created and store in the firebase database.
When the date and time of meeting is passed
Firebase should automatically delete child nodes of that group.
I'm thinking of cloud function , but don't know how to make this work.
On top of my head, you can do three things:
1) Cloud Functions: You correctly pointed it out that a cloud function is the correct way. However, a cloud function will need a trigger as far as I understand. You can run this trigger on a daily basis for instance to clear all meetings after due date. Potential way to run this trigger can be a cron job from a website like https://www.easycron.com/
2) Event Listener on a client: If you have a listener fixed on a client then you can put a check each time its run. So any client running their own event will clear it based on a check. It will not clear automatically after time passes but will clear once a relevant client listens for it.
3) Ignore meetings after due dates on a client. That way you save the old data as well. Might be costly in terms of bandwidth in the long term.
Also, if you have just started, I'd personally advice to use Firebase Firestore instead. I found it much better to use.

Can I use Parse and Firebase at the same time?

I like the simplicity of Parse to handle the user side of things as well as push notifications and all the other cool features, but I also want to use Firebase to make use of the real time database for the widely multiplayer game aspect of the app.
yes you can use them both. But if you wanna use this two at the same time at the one variable/data it can be a problem. Just use them with the separate data e.g=Firebase - realtime event handling, Parse - save/load score & notifications
Yes you can use it. There are many apps available that are using time for real time. Check out this link

Store on database vs Retrive data from network

I working on my app that uses "The movie database API".
This app just parse a json from the API and gets each movie information.
My question is what the better way to that.
First alternative is to build syncAdapter, check for updates each day for example, store all movies information on local database and update the UI from database.
Second alternative is just retrive the information from network on each request and update the UI.
Can anyone explain me wich way is better and why?
Absolutely the first alternative !!!!!
let's Suppose your app is used by thousand of users and you have chosen the second one .....oh poor server :)
I usualy create my own local database and syncronize it each "x" interval time depending on the type of information : if I have, for example, a list o category that I know they are rearly changed than I syncronize them each "2 days" ....For data that can be different more often I use 20 minutes and so on. This just avoid stressing server if the user goes in/out from your app.
Bye

Categories

Resources