2

I have a node in Firebase Realtime Database which saves a boolean value true or false along with some more attributes. If and only if this key is false, then I allow the user to perform write operations on the same node in database.

When user has did the task, this value is again set back to false. This is like the critical section concept of the operating systems i.e., only one user can perform a task at a time.

It works as I intended, but the issue I am facing is if user has changed this value to true, and but due to bad network or something he/she is not writing on the database. Now, no one can write on that node on database.

I would like to add a time-interval functionality, if user has not performed any write operation for some interval, say 10 minutes and the boolean is true. Then, I would like to set it to false.

I know that Firebase Cloud Function triggers only on database write, update and create operations. Please suggest me something to handle this issue or some other ways by which I can perform it. There is no code snippet regarding this functionality anywhere. I have looked up various resources on internet, brainstormed myself, I could get nothing.

James Z
  • 12,209
  • 10
  • 24
  • 44
Raj K.
  • 21
  • 1
  • 1
    Maybe you should take a look at cron jobs or [schedule functions](https://firebase.google.com/docs/functions/schedule-functions). BTW I'm not too familiar with it, but it might be helpful to you. – DHAVAL A. Jan 08 '20 at 04:24
  • This is pretty close to the time-to-live scenario that Doug described here: https://medium.com/firebase-developers/how-to-schedule-a-cloud-function-to-run-in-the-future-in-order-to-build-a-firestore-document-ttl-754f9bf3214a. Also see my answer here: https://stackoverflow.com/questions/47173560/is-there-any-ttl-time-to-live-for-documents-in-firebase-firestore – Frank van Puffelen Jan 08 '20 at 04:43
  • 1
    I also looked up at scheduling functions, but in my view, it would not work at all the time as we anticipate since the boolean may not be deleted exactly at the time we want. Consider this example : Let us suppose that at some time T, the node was written few moments ago by the user, and the cloud function sees that it was written just few moments earlier (Current TImestamp - Node's Time Of Updation Timestamp). Now clearly, scheduled function would run (say 10 minutes ) later. Now, at but at time T+1 minute, the user has stoped working. At time T+10, node's boolean would not set.. – Raj K. Jan 08 '20 at 05:24
  • 1
    .. to false since 9<10. After next 10 minutes, at time T+20, now, the function would see that it has not been updated for 19 minutes and then set it to false. But this could be overcomed by scheduling function for each (Say 1 minute) very short time. In this case, function would be invoked too much times which would not be beneficial to both invoking cloud server and me as well . – Raj K. Jan 08 '20 at 05:27

1 Answers1

0

This might not be perfect solution, but it is a workaround. At the place, where you are allowing data write, probably through a conditional check; you can simply add another condition by using OR operator. Simply allow data write when (that Boolean value is false) or (boolean value is true but the node's last updation timestamp is more than current timestamp by your time of interval). It should me something like this : if(!dataSnapshot.getBoolean || (dataSnapshot.getBoolean && currentTimestamp - dataSnapshot.getTimestamp >= YOUR_TIME_INTERVAL)){ //Your Logic Here }

Anuj Kumar
  • 1,092
  • 1
  • 12
  • 26
  • Why Did I not thought of that ?? This simple trick may help but then I need to somehow stop the previous user to stop updating node when he/she comes back with good network. Still, I need perfect solution, otherwise I would have to try this. – Raj K. Jan 08 '20 at 06:19