如何检查特定的 FirebaseFirestore 错误代码?

How to perform a check for a particular FirebaseFirestore error 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
                    }
                }
        );

如果集合已经存在,我想更新我的 Firestore 数据库。否则,我希望 Firestore 创建一个新集合。

我只想在找不到该集合时才创建一个新集合。本质上,我想像这样进行检查:

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
                            }
                        }
                    }
            );

我怎样才能实现这个目标?

如果您将异常转换为 feedbackFirebaseFirestoreException object and then check its getCode() value against the possible Firestore error codes,那应该是可能的。

所以像这样:

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
    }
  }
}