I have a value that is set to "1" after a particular task is completed but I want it to set back to "0" after 24hour from the period the task was completed.
Asked
Active
Viewed 1,782 times
2
-
used work manger for background service. – Dec 06 '18 at 13:15
-
can you please elaborate, I am new to this Android field – Avinash Singh Dec 06 '18 at 13:17
-
read about background service. – Dec 06 '18 at 13:20
1 Answers
3
To solve this problem, I recommend you to use Cloud Functions for Firebase :
Cloud Functions for Firebase lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your code is stored in Google's cloud and runs in a managed environment.
As you can see, you can simply interact with Firebase realtime database.
If you want to schedule a Cloud Function to run 24 hours, I recommend you see Frank van Puffelen's answer from this post, where he explains how you can do this.
So you can run a database query to find all objects with a timestamp between "now" and 24 hours ago, and update them.
In code, it should look similar to:
var now = Date.now();
var 24hoursago = now - ...;
var ref = firebase.database().ref("yourSpecificNode");
var query = ref.orderByChild("timestamp").startAt(24hoursago).end(now);
query.once("value").then(function(snapshot) {
snapshot.forEach(function(user) {
user.ref.update({ yourSpecificProperty: "0" });
});
});

Alex Mamo
- 130,605
- 17
- 163
- 193