Skip to content
Advertisement

Limiting document fields and race conditions – Firebase Firestore Android

I am using Firestore for Android right now; recording some data and writing it to documents. Instead of creating document every time, I want to limit documents like 10 or some x number of fields. If a user writes to document and field count reaches x, the user will create new document in the same collection.

So my question is: How to avoid race conditions when creating new documents(multiple documents created by different users)? Is it feasible to use this method for split this kind of data to different documents?

Advertisement

Answer

How to avoid race conditions when creating new documents(multiple documents created by different users)?

Always consider using Firestore transactions:

Transactions: a transaction is a set of read and write operations on one or more documents.

This means that if multiple users will try to add/delete/update data to/from a shared document at the same exact time, you’ll always have consistent data. And to answer the second question:

Is it feasible to use this method to split this kind of data into different documents?

Why now? This can be feasible as long as you stay below the maximum limitation of 1Mib / document.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement