0

I need to create a Firestore document, and use it's documentID in the next step. This is all fine, but I want the app to work offline, so I can't create the document offline and query it's ID.

I thought that I could set the docID to be the DateTime object on which the docuement was created. That way it could not be accidentally recreated later and overridden(because they would need to get the millisecond right).

This way I could create the document, and use it's id to create more documents in the next step. And when I get internet connection everything will sync with Firebase.

Is there anything I'm missing, or any reason why this system will fail?

Pieter Pienaar
  • 121
  • 1
  • 9

2 Answers2

1

The doc() method, which returns a DocumentReference, does not need the client to be online: it is always executed locally in the front-end without contacting the back-end (You will note that consequently it is not asynchronous).

So you can very well rely on it to generate a unique Firestore Document ID: call it without any path and use the id property of the DocumentReference you get. Even if this is done on a device being offline, you can be sure the Firestore Document ID will be unique.


More details on Firestore Document IDs in this SO answer.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
0

This does not ensure you'll have uniqueIds, as there's a small, but real possibility that another user creates a document and the very same moment. For creating uniqueids, you could use Uuid library instead

Luis Utrera
  • 942
  • 6
  • 15