0

I have the following code:

db.collection("property").document(userUID).update(propertyRoot).addOnSuccessListener( new OnSuccessListener<Void>(){ @Override public void onSuccess(Void aVoid) { //Success } } ).addOnFailureListener( new OnFailureListener(){ @Override public void onFailure(@NonNull Exception e){ //Create a new collection and set } } ); 

I would like to update my Firestore database if the collection already exists. Otherwise, I would like Firestore to create a new collection.

I would like to create a new collection only if the collection is not found. Essentially, I would like to do the check like this:

db.collection("property").document(userUID).update(propertyRoot).addOnSuccessListener( new OnSuccessListener<Void>(){ @Override public void onSuccess(Void aVoid) { //Success } } ).addOnFailureListener( new OnFailureListener(){ @Override public void onFailure(@NonNull Exception e){ if(e.errorCode == FirebaseFirestoreException.NOT_FOUND) { //Create a new collection and set } } } ); 

How can I go about achieving this?

1
  • I think this answer might help. Commented May 27, 2022 at 5:05

1 Answer 1

1

That should be possible if you cast the exception to a feedbackFirebaseFirestoreException object and then check its getCode() value against the possible Firestore error codes.

So something like:

public void onFailure(@NonNull Exception e){ if (e is FirebaseFirestoreException) { if ((e as FirebaseFirestoreException).getCode() == FirebaseFirestoreException.Code.NOT_FOUND) { //Create a new collection and set } } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.