0

In My project I want user to read a data from firebase realtime data at same time he will mark data as locked until he finishes some operations, during this time other users cannot read this data.

Anther scenario if the lock is not possible is once he read he at same time write some data to indicate that data as locked. If anther user read this data he knows its locked.

I am not sure if this is possible some how in fire base.

Is there any Idea how to use this with firebase realtime db.

Thudner
  • 1,043
  • 1
  • 7
  • 14
  • You're pretty much already describing the solution as far as I can see: you'll need to store a list of list of who locked what data, and possibly also the inverse: a list of what data is locked by whom. Also see https://stackoverflow.com/questions/41527058/many-to-many-relationship-in-firebase I recommend you give it a try, and post back if you get stuck with the [minimal, complete/standalone code that allows any of us to reproduce where you got stuck](http://stackoverflow.com/help/mcve). – Frank van Puffelen May 16 '20 at 13:20

1 Answers1

0

There isn't any out-of-the-box solution for your use case (set a lock that restrict read/write access rights) but you could easily built such a system. Since you didn't indicate which language you are using, I describe below a generic approach with links corresponding to the JS SDK:

  • You first define a Security Rule that allows users to read/write to a database node under two conditions: (a) A specific "readWriteLock" field (i.e. a sub-node) has the value "all" -> All users can read from & write to the DB node, OR (b) The "readWriteLock" field has a value corresponding to the uid of a specific user -> Only the user with this uid can read from & write to the DB node.
  • Then, when you want to lock a specific DB node, you first need to write to the desired document in order to set the value of the "readWriteLock" field to the user's uid. This should be done in a Transaction.
  • Then you fetch the document (only the user who locked it can read it)
  • Then, when the "operations" you mention ("mark data as locked until he finishes some operations") are finished, you set back the value of the "readWriteLock" field to "all"
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • Thank you, Now the question is what if 2 users same time trying to read unlocked data. In this case I will stuck with my application scenario. How I can solve this – Thudner May 16 '20 at 20:23
  • See the update: you just have to restrict write **and read** access. (Actually this was my initial answer and I modified it, thinking other users could read...) – Renaud Tarnec May 16 '20 at 21:05
  • @Thudner Don't forget to leverage the .onDisconnect to reset that. Otherwise if the user disconnects and doesn't return the 'locked' node will stay forever locked. – Jay May 17 '20 at 13:42
  • @Thudner Hi, did you have time to try the proposed solution? – Renaud Tarnec May 21 '20 at 13:23
  • Not Yet, I will let you know once I do it – Thudner May 22 '20 at 13:37