Is It Possible To Get The ID Before It Was Added?

Tal Barda picture Tal Barda · Oct 20, 2017 · Viewed 58k times · Source

I know that in Realtime Database I could get the push ID before it was added like this:

 DatabaseReference databaseReference= FirebaseDatabase.getInstance().getReference();
 String challengeId=databaseReference.push().getKey();

and then I could add it using this ID.

Can I get it also in the Cloud Firestore?

Answer

34m0 picture 34m0 · Oct 20, 2017

This is covered in the documentation. See the last paragraph of the add a document section.

DocumentReference ref = db.collection("my_collection").doc();
String myId = ref.id;