0

I want to return value from the inbuilt void method. How can I do so?

code

 public static boolean upload(DatabaseReference reference, String path, String userUid, String value)
    {

        reference.child(path).setValue(value).addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void unused) {
                return true;
            }
        })

        return false;
    }

Error:

Cannot return a value from a method with void result type

Edit:

Actually, the code that I am currently using which is not readable at all I know is

  DatabaseReference reference = mDatabase.getReference().child(FirebaseVar.USERS).child(mUser.getUid());

                        reference.child(FirebaseVar.NAME).setValue(binding.nameSignUp.getText().toString()).addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void unused) {
                                reference.child(FirebaseVar.ROLE).setValue(selectedRoles).addOnSuccessListener(new OnSuccessListener<Void>() {
                                    @Override
                                    public void onSuccess(Void unused) {
                                        reference.child(FirebaseVar.BRANCH).setValue(selectedBranch).addOnSuccessListener(new OnSuccessListener<Void>() {
                                            @Override
                                            public void onSuccess(Void unused) {
                                                reference.child(FirebaseVar.HOSTEL).setValue(selectedHostels).addOnSuccessListener(new OnSuccessListener<Void>() {
                                                    @Override
                                                    public void onSuccess(Void unused) {
                                                        reference.child(FirebaseVar.GENDER).setValue(checkedbutton.getText().toString()).addOnSuccessListener(new OnSuccessListener<Void>() {
                                                            @Override
                                                            public void onSuccess(Void unused) {
                                                                Toast.makeText(SignUpDetailActivity.this, "Success", Toast.LENGTH_SHORT).show();
                                                            }
                                                        });
                                                    }
                                                });
                                            }
                                        });
                                    }
                                });
                            }
                        });

SO my question is how to improve my given code?

  • Firebase database operations are asyncronous and return immediately. The listener you provide won't be invoked until some time later, after your `upload` method has already returned. You can't make the listener return a value from the enclosing `upload` function. – Doug Stevenson Sep 10 '22 at 12:55
  • 2
    You’ll have to use a callback for this. – Darshan Sep 10 '22 at 13:01
  • @DougStevenson yes Doug so actually my problem is I want to set many values in my firebase database and after all, the values are successfully uploaded then I want to show a message that says "successfully uploaded" so with the help of addOnSuccesListner i can only check one value either set or not but how can I check simultaneously wether all values successfully uploaded or not? – GajjarSVNIT Sep 10 '22 at 13:01
  • 1
    That sounds like a different question than the one you posted. Maybe you would like to edit the question to better explain what you're trying to accomplish and where you are stuck implementing that. – Doug Stevenson Sep 10 '22 at 13:27
  • @DougStevenson I posted both the questions. – GajjarSVNIT Sep 10 '22 at 13:41
  • I also think that this [resource](https://medium.com/firebase-tips-tricks/how-to-read-data-from-firebase-realtime-database-using-get-269ef3e179c5) will help. Isn't about adding data, but reading, but I think you'll get the idea, as both operations are asynchronous. – Alex Mamo Sep 11 '22 at 08:22

0 Answers0