1

I am using the new Firebase console. I am trying to save an object to the database but its not getting saved. Following is the code.

DatabaseReference mFirebaseRef = FirebaseDatabase.getInstance().getReference();
mFirebaseRef.push().setValue(object, new DatabaseReference.CompletionListener() {
            @Override
            public void onComplete(DatabaseError databaseError, DatabaseReference reference) {
                if (databaseError != null) {
                    Log.e(TAG, "Failed to write message", databaseError.toException());
                }
            }
        });

the 'object' is created properly. There is no error I am getting and I have checked in debugging that the onComplete method is not getting triggered. Security Rule is also true for write.

2
  • Can you show how you create object? Or at least the minimal object with which you can reproduce the problem? Commented May 26, 2016 at 20:32
  • object is a java class reference. Object object = new Object(params). The code is not working with Hash Map also. Commented May 27, 2016 at 7:41

2 Answers 2

3

Note that DatabaseReference.CompletionListener fires "when an operation has been acknowledged by the Database servers". Is it possible you did not have a network connection at the time you ran your test? I copied your code and ran it successfully on a phone with object defined like this:

Object object = new String("Test");

I then enabled airplane mode, re-ran the code and observed the behavior you describe. The completion listener did not fire until I disabled airplane mode and a network connection was established.

Example code for checking the status of your network connection is provided in this documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

I did not have the Internet permission in manifest. Thanks, It was a silly mistake.
0

use the below code:

mFirebaseRef.push().setValue(object).addOnCompleteListener(new OnCompleteListener<Void>() 
{
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                           if(task.isSuccessful()){
                               // write code for successfull operation
                           }
                            }
                        });
                    }

.push will generate a random key on which your data will stored if you do not use .push it will overwrite on previous stored data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.