Skip to content
Advertisement

How to run a background Cloud Function and schedule a change in database?

I’m developing an app using React Native and Firebase Realtime database. I want to do something like this.

From the app, I set a time duration and press a button. Then, it writes data to the Firebase. Assume that I set the time duration to 1000000000 ms and press the button. Then, the database looks like this:

schedules
  |
  |--schedule1
       |
       |--status: "Running"
       |--durationInMS: 1000000000

I want to run a background function that changes the above status to “TimeOver” after 1000000000 ms. This function should be run in the background even if the app is closed:

schedules
      |
      |--schedule1
           |
           |--status: "TimeOver"
           |--durationInMS: 1000000000

How to do this?

Advertisement

Answer

There is no built-in trigger to run after a certain delay that is written in the database.

But you can either build your own scheduler using Cloud Scheduler, or create a regularly running scheduled function that then checks for expired schedules in your database.

Also see:

Advertisement