Reputation: 57
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?
Upvotes: 3
Views: 3524
Reputation: 599491
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?
Upvotes: 2
Reputation: 1546
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.
Upvotes: 3