Getinstance() Doesn't Work with Other Location Than Us-Central1 in Realtime Database

getInstance() doesn't work with other location than us-central1 in Realtime Database

To get a reference to a Firebase Realtime Database other than the (default) "us-central1", for instance "europe-west1", you must pass the database URL to the getInstance() method.

For "us-central1" there is no need for doing that, you can simply call the "getInstance()" method without passing any arguments.

For more info, please check the official documentation regarding Realtime Database locations:

  • https://firebase.google.com/docs/projects/locations#rtdb-locations

As an alternative solution, you can download an up-to-date google-services.json from the Firebase Console and add it to your app. If the correct URL of the location is in there, the SDK will read it from there when you call FirebaseDatabase.getInstance() without passing any arguments.

Unable to write in firebase realtime database with Singapore asia-southeast1 as location

In order to write some data into the Firebase Realtime Database, you need to use DatabaseReference#setValue(Object value). In code should look like this:

val database = Firebase.database
val myRef = database.getReference("message")
myRef.setValue("Hello, World!")

You might also attach a complete listener to see the result of the write operation.

Edit:

In order to use the following reference:

val usersRef: DatabaseReference = FirebaseDatabase.getInstance().reference.child("Users")

You need to create rules to allow access. Since you are using "users" instead of "Users":

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

Firebase servers will reject the write. So please use the following rules:

{
"rules": {
"Users": { //Use capital letter U
".read": true,
".write": true
}
}
}

Edit2:

Final change. Adding the URL location of the database fixed the problem.

  • getInstance() doesn't work with other location than us-central1 in Realtime Database

Data is not written into the Firebase Database from Android

To be able to add another object under your "orders" node, please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ordersRef = rootRef.child("orders");
Map<String, Object> order = new HashMap<>();
order.put("info", "No additional information");
order.put("name", "Another Test Order");
order.put("table", "15");
ordersRef.child("order_2").setValue(order).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d("TAG", "Data successfully written.");
} else {
Log.d("TAG", task.getException().getMessage());
}
}
});

This code will work only if you set the proper security rules in your Firebase console:

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

Remember to keep these rules only for testing purposes. Besides that, you can also attach a complete listener to see if something goes wrong.

If you want to use DatabaseReference#push() method, then you should use the following line of code:

ordersRef.push().setValue(order);

Edit:

According to your last comment:

Database location: Belgium (europe-west1)

You should check my answer from the following post and add the correct URL to the getInstance() method:

  • getInstance() doesn't work with other location than us-central1 in Realtime Database

In your particular case it should be:

DatabaseReference rootRef = FirebaseDatabase.getInstance("https://drink-server-db-default-rtdb.europe-west1.firebasedatabase.app").getReference();

I don't see any data in my Realtime Database in Firebase's console

When you're using the following line of code:

gameRef.setValue(g);

The data you're trying to write into the database may succeed or fail, but you'll never know that since you aren't attaching a listener to check that. So solve this, you have to attach a listener as in the following lines of code:

gameRef.setValue(g).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d("TAG", "The operation is complete.");
showkey.setText(g.key);
} else {
Log.d("TAG", task.getException().getMessage());
}
}
});

If the operation fails, most likely the Firebase servers rejected your operation. So make sure you have the proper rules. Otherwise, set the key to the TextView. Remember that all Firebase APIs are asynchronous. This includes the write and the read operations. If you need later to read the data, I recommend you check the following article:

  • How to read data from Firebase Realtime Database using get()?

How to read data from Firebase Database? - Java

To be able to read the data under the jjjj node, please use the following lines of code:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference nameRef = db.child("players").child("jjjj");
nameRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
DataSnapshot snapshot = task.getResult();
String loses = snapshot.child("loses").getValue(Long.class);
String name = snapshot.child("name").getValue(String.class);
String wins = snapshot.child("wins").getValue(Long.class);
Log.d("TAG", loses + "/" + name + "/" + wins);
} else {
Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
}
}
});

The result in the logcat will be:

3/jjjj/4

Things to notice:

  • Always create a reference that points to the node that you want to read.
  • If your database is located in another lcoation than the default, check this answer out.


Related Topics



Leave a reply



Submit