There's a Firebase database associated with my project. When the value changes to 2 then after 10 minutes I want the same field to update to 1. The project is on android. Is there any way to do it? For creating a specific time interval?
Asked
Active
Viewed 3,137 times
3
-
I don't know what You have tried. You can use java timer to poll the DB periodically. – Yohannes Gebremariam Jul 10 '17 at 13:06
-
You can create one service which run in background and update firebase after 10 minutes. – Ronak Joshi Jul 10 '17 at 13:07
-
1Seems like a valid use case for observables. – Aluan Haddad Jul 10 '17 at 13:09
-
1Please provide more details. You can use timer class in Android, or deploy a Firebase Function in JavaScript. – Bugs Buggy Jul 10 '17 at 13:16
-
@Ronak Joshi, but i want to update only when the value changes to 2. Should work like a trigger with timer. – 1729i Jul 10 '17 at 13:34
-
Is there any way to create a Firebase function which actike a trigger? – 1729i Jul 10 '17 at 13:35
2 Answers
2
I would suggest you to write a JavaScript function which writes the new value after 10 minutes, and write a Firebase write event trigger to call that function whenever you see a change in value and the value as 2.
If you write this functionality in Android App, it may not update in time if the user disconnects the internet from his phone.
Documentation for Firebase Function
Read this documentation. It is fairly easy and an integral part of Firebase as a backend for Applications.

Community
- 1
- 1

Bugs Buggy
- 1,514
- 19
- 39
-
This is totally accurate but there is a problem. We cannot call our cloud function with a delay, I mean maybe 10 minutes okay but 10 hours is problem because of timeout, pub/sub functions are not the solution neither since it supports only scheduling calls, we need to call our function exactly once after some delaying, am I missing something or there is not a solution for that really? – uerden Nov 11 '21 at 12:53
-
The solution to that is, put a listener for new values, and every time a new value is added, a new thread will start as a result of the listener. Either create a scheduled service, and submit the new value to that, or simply put the listener thread to sleep for the given time. – Bugs Buggy Nov 11 '21 at 13:18
-
Thank you Bugs Buggy for your quick reply. So you say there is no solution for cloud functions, we should create our services? – uerden Nov 11 '21 at 13:27
-
No no. You can simply write methods for it, that actually gets executed after the given time. I haven't worked with Node in a long time, but there is a util in Java which lets you create "ScheduledThreads". Do it in your cloud functions only. – Bugs Buggy Nov 12 '21 at 08:36
2
You could do this with Cloud Functions for Firebase, or alternatively with a timer in Android:
final DatabaseReference ref = ...;
ref.addListenerForSingleValueEvent(new ValueEventListener() {
void onDataChanged(DataSnapshot snapshot) {
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
ref.set(1);
}
},
10*60*1000);
}
...
Inspired by What is the equivalent to a JavaScript setInterval/setTimeout in Android/Java?

Frank van Puffelen
- 565,676
- 79
- 828
- 807