简体   繁体   中英

Can anyone tell me where i am wrong in this condition?

I am trying to restrict that if the program name already exists and the concatenated value does not exist then push the concatenated value otherwise do nothing but still, my concatenated value is getting pushed into the database.

Here is the code:

public void addProgramAndDepartmentName(String programName, String departmentName) {    
    final DatabaseReference rootRef;
    rootRef = FirebaseDatabase.getInstance().getReference();

    String programAndDepartmentName = programName + departmentName;

    rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            if ( !(snapshot.child("Program & Departments").child(programName).exists()) ) {    
                rootRef.child("Program & Departments").child(programName).push().child("Program Name").setValue(programAndDepartmentName).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        /*AddCourses addProgramNameToSpinnerInAddCourse = new AddCourses();
                        addProgramNameToSpinnerInAddCourse.getDataIntoSpinnerFromFirebase();
                        addProgramNameToSpinnerInAddCourse.spinnerArrayAdapter.notifyDataSetChanged();*/

                        if (!task.isSuccessful()) {    
                            Toast.makeText(AddPrograms.this, "Program and Department is not Added into Database.", Toast.LENGTH_SHORT).show();

                        }    
                        else {    
                            Toast.makeText(AddPrograms.this, "Program and Department name is added into Database if.", Toast.LENGTH_SHORT).show();
                            loadingBar.dismiss();    
                        }
                    }
                });
            }
            else if( (snapshot.child("Program & Departments").child(programName).exists() ) &&
                    ( !( snapshot.child("Program & Departments").child(programName).child("Program Name").child(programAndDepartmentName).exists() ) ) ) {
                rootRef.child("Program & Departments").child(programName).push().child("Program Name").setValue(programAndDepartmentName).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task)  {    
                        /*AddCourses addProgramNameToSpinnerInAddCourse = new AddCourses();
                        addProgramNameToSpinnerInAddCourse.getDataIntoSpinnerFromFirebase();
                        addProgramNameToSpinnerInAddCourse.spinnerArrayAdapter.notifyDataSetChanged();*/

                        if (!task.isSuccessful()) {

                            Toast.makeText(AddPrograms.this, "Program and Department is not Added into Database.", Toast.LENGTH_SHORT).show();

                        }    
                        else {    
                            Toast.makeText(AddPrograms.this, "Program and Department name is added into Database else if.", Toast.LENGTH_SHORT).show();
                            loadingBar.dismiss();    
                        }
                    }
                });
            }    
            else {    
                Toast.makeText(AddPrograms.this, "This " + programName + " already Exists.", Toast.LENGTH_SHORT).show();
                loadingBar.dismiss();    
            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {    
        }
    });    
}

but the only condition that is working correctly is is first one the condition after And is not working

And here is my JSON file:

{
  "Program & Departments" : {
    "Bs" : {
      "-MV4Heye6EkKPMFlL9IX" : {
        "Program Name" : "Bscs"
      },
      "-MV4Hfy_MHv0CVCnlsRZ" : {
        "Program Name" : "Bscs"
      }
    }
  }
}

please guide me where I am wrong?

If you want a single node with a specific value to exist, make that value the key of that node:

{
  "Program & Departments" : {
    "Bs" : {
      "Bscs": true
    }
  }
}

With this structure, the path /Program & Departments/Bs/Bscs is guaranteed to exist only once, since the JSON can only contain that path only once.

You can then check if the path exists, and create it if not, with a single transaction:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child(programName).child(programAndDepartmentName);
ref.runTransaction(new Transaction.Handler() {
    @Override
    public Transaction.Result doTransaction(MutableData mutableData) {
        if (!mutableData.exists())
            mutableData.setValue(true);
        }
        return Transaction.success(mutableData)
    }

    @Override
    public void onComplete(DatabaseError databaseError, boolean committed, DataSnapshot currentData) {
        Log.d(TAG, "runTransaction:" + databaseError);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM