Reputation: 129
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.
Upvotes: 5
Views: 2483
Reputation: 21
I've had the same problem and I got the answer hope it's not too late for others.....
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.....
in short just redownload and replace google-services.json again and clean - rebuild project....
Upvotes: -1
Reputation: 9
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!
Upvotes: 0
Reputation: 27
DatabaseReference ref = FirebaseDatabase.getInstance("<<Insert Databse URL here>>").getReference();
Upvotes: 0
Reputation: 559
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!")
Upvotes: 3
Reputation: 129
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.
Upvotes: 6