6

I am trying to use the realtime database for the first time. The set value function is not working for me.

I already updated the writing rules in the following way :

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

My code looks like this:

        Integer Age = 24;
        FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
        DatabaseReference sReference = mDatabase.getReference();
        sReference.child("Users").child("George").child("Age").setValue(Age)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Toast.makeText(Dashboard.this, "Data stored", Toast.LENGTH_LONG).show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(Dashboard.this, "Error", Toast.LENGTH_LONG).show();
                    }
                });

When I print out the reference it is correct. However neither the OnSucces nor the OnFailure is triggered.

Thank you for your help.

6
  • Could it be the events are attached to it later than the action executed, is the set happening sooner? Could do it the way the tutorial is recomending? firebase.google.com/docs/database/web/read-and-write Commented Dec 12, 2020 at 13:00
  • If neither the OnSucces nor the OnFailure is triggered, then is most probably you don't have an internet connection on the device. Commented Dec 12, 2020 at 13:37
  • @AlexMamo I think the Internet connection is not the problem, as the authentification works fine and I also tried it on the emulator Commented Dec 12, 2020 at 14:38
  • @AntonKrug it is the first time the set is happening, tried it with various template writing codes as well Commented Dec 12, 2020 at 14:38
  • 1
    Can you enable debug logging and check the logcat output of your app of the moment when the setValue call happens for any relevant messages? Commented Dec 12, 2020 at 15:40

5 Answers 5

6

Found the problem. The database is located in Europe. Apparently I did not specify the URL in the getInstance() method, which seems to be required in this case.

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

4 Comments

Great you posted the answer after figuring it out so if somebody has a similar issue they will not find a 'solved' question without an answer.
Thank you @Alex. I couldn't figure this out for 8 hours.
working perfectly for me, Database location: Singapore (asia-southeast1)
In 2024 this is still an issue. Database location: europe-west1. This actually fixed the issue for me
3

This is probably a firebase realtime database bug. It happens when you select Europe server (beta) instead of USA. And occures with no errors or warning of any kind.

Fix as mentioned above. Use direct link from firebase console in you database instance (change xxx with your project data from firebase console realtime database section):

// Write a message to the database
val database = Firebase.database("https://xxxxxxxx-xxxxxxx-default-rtdb.europe-west1.firebasedatabase.app/")
val myRef = database.getReference("message")

myRef.setValue("Hello, World!")

1 Comment

This... I can't believe I have wasted an entire day not understanding why my database was not working at all... no logs, nothing. Thank you sir, you have probably saved me even more wasted time. Google gives absolutely no warning for this when setting other db locations, how unprofessional, they think that somehow we can imagine the cause of these issues out of the blue.
0
  1. Ensure your sha1 is added to firebase
  2. Enable signIn method in Firebase
  3. Enable Android Device Verification in Google Cloud
  4. Ensure internet permission in AndroidManifest.xml
  5. Provide db url in getInstance(). How to find Database URL
    DatabaseReference ref = FirebaseDatabase.getInstance("<<Insert Databse URL here>>").getReference();
    
  6. Restart your emulator and modem/router
  7. debug with logs

Comments

0

Please make sure you are specifying database URL while getting database instance like -

val database = Firebase.database("<url_here>")
val ref = database.reference

and then perform setValue() function over it say -

ref.child("users").child(uid).setValue(user)

This worked in my case!

Comments

-1

I've had the same problem and I got the answer hope it's not too late for others.....

  1. After Creating Realtime database go to Project Settings in firebase dashboard -> Project Overview -> Gear Icon -> Project Settings and scroll down and check google-services.json file and download it again and replace with same place in android studio -> select project view -> in app folder (you know the drill) .... and clean - rebuild app again...... it worked for me after hours.....

  2. in short just redownload and replace google-services.json again and clean - rebuild project....

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.