0

My question might be really simple but i will appreciate any help, am using java and firebase and i have a variable in the database called count == 0, i need to increment this value every day by 1, so after 30 days the variable count should be count == 30, the only trouble is making that increment programmaticaly.

Any suggestions will be highly appreciated.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
John Kaita
  • 15
  • 9
  • `UPDATE MY_TABLE SET COUNT = (COUNT +1) WHERE ID = 1234` – RobOhRob Oct 16 '19 at 20:02
  • Have some scheduling task run this every day – RobOhRob Oct 16 '19 at 20:03
  • What solution have you already tried, and why didn't that solution satisfy you? – M. Prokhorov Oct 16 '19 at 20:15
  • So you want to execute a task (in your case, incrementing a number in a database) daily. I'd suggest searching for topics like "android schedule job" (or task) – Bernhard Stadler Oct 16 '19 at 20:16
  • 1
    How about storing a `created_at` using `System.currentTimeMillis()` and then calculate the time from `created_at` to `Instant.now()` in days? No scheduling that can fail, no counter that can be manipulated... https://stackoverflow.com/questions/37976468/saving-and-retrieving-date-in-firebase – Andreas Oct 16 '19 at 20:21

1 Answers1

1

A scheduled Cloud Function deployed with the Firebase CLI will be the proper way to update your data in firebase. Your can run a scheduled function which can be triggered at a specific time (everyday or week however you want) and you can update your variable in the database inside the function. Here is a sample from firebase official documentation :

exports.scheduledFunctionCrontab = functions.pubsub.schedule('5 11 * * *')
  .timeZone('America/New_York') // Users can choose timezone - default is America/Los_Angeles
  .onRun((context) => {
  console.log('This will be run every day at 11:05 AM Eastern!');
  return null;
});

Read documentation here : https://firebase.google.com/docs/functions/schedule-functions#write_a_scheduled_function

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Alif Hasnain
  • 1,176
  • 1
  • 11
  • 24