Home > database >  Firebase Realtime - Transaction with additional variables
Firebase Realtime - Transaction with additional variables

Time:01-04

So I know how to use transaction to increment or decrement a number. But how do I know who sends this number?

For example, there's a plate of cookies and everyone grab one, almost if not at the same time. How do I get the person's id or name that grabs the last cookie?

CodePudding user response:

There's nothing built into the SDK or product for this, so you'll have to build it yourself on top of those. For example, if you:

  • Have every user write their name/UID when they grab a cookie.
  • Reject writes in security rules when there are no cookies left.

Then:

  • The name/UID that is in the database once no cookies are left will be the person who grabbed the last cookie.

There are many variations possible on this scheme, so pick whatever meets your use-case. The important thing is that you'll need to write the user name/UID in addition to their action.

  • Related