Based on this SO answer I came to know that firestore does not have collection level locking in a transaction. In my case, I have to ensure that the username field in users collection is unique before I write to a collection. For that, I write a transaction that does this:
Now the issue here is that if two clients simultaneously try to run this transaction, both might query the collection and since the collection is not locked, one client might insert/update a document in collection while other won't see it.
Is my assumption correct? And if yes, then how to deal with such scenarios?
--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/7063528c-3f3e-4f08-81e0-3c1b26fd14d6o%40googlegroups.com.
Hi Rahul,If you do the read ops as part of the transaction, then it's atomic and will fail / retry if the data changed between read and write.Note that there are probably much simpler ways to implement this. One that comes immediately to mind is just creating the collections based on the user's authenticated uid. Guaranteed unique and only writable by the uid in question as long as you add a security rule.Assuming your goal here is to "claim" a username and it may not be unique, you could do that by maintaining a map of username => uid. When someone attempts to claim it, they write their uid into the mapping for that username. If it has already been written, a rule prevents this. Transaction entirely optional in this case.☼, Kato
On Tue, Aug 4, 2020 at 8:51 AM Rahul Priyadarsi <rahul...@gmail.com> wrote:
--Based on this SO answer I came to know that firestore does not have collection level locking in a transaction. In my case, I have to ensure that the username field in users collection is unique before I write to a collection. For that, I write a transaction that does this:
- Executes a query on users collection to check if a document exists where username=something
- If it does exist, fail and return error from transaction
- If it does not exist, just run the write operation for the userId I want to update/create.
Now the issue here is that if two clients simultaneously try to run this transaction, both might query the collection and since the collection is not locked, one client might insert/update a document in collection while other won't see it.
Is my assumption correct? And if yes, then how to deal with such scenarios?
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/7063528c-3f3e-4f08-81e0-3c1b26fd14d6o%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/f9c586bb-7566-4606-899a-b31e5270fda4o%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/f9c586bb-7566-4606-899a-b31e5270fda4o%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/0c8f1414-1285-452f-a0b3-1f980b63dbf4o%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/CAHafJBrtoTcaiOGF2L_McJFH_Wb3G6RA8BZCiD1g9JCPXK-FPA%40mail.gmail.com.