getInstance() doesn't work with other location than us-central1 in Realtime Database
I am trying to save user credentials to Firebase Realtime Database. However, when I execute the program, the DB does not update. I have configured the Firebase setup correctly as Authentication and Storage (both Firebase) are working.
build.gradle
:
dependencies {
...
// Firebase SDK setup
// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:28.0.1')
// Declare the dependency for the Firebase library
implementation 'com.google.firebase:firebase-storage-ktx'
implementation 'com.google.firebase:firebase-auth-ktx'
implementation 'com.google.firebase:firebase-database-ktx'
}
apply plugin: 'com.google.gms.google-services'
AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package=<NAME>>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
...
</application>
</manifest>
The registration class (MWE):
class RegisterActivity : AppCompatActivity() {
var selectedPhotoUri: Uri? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Register UI
register_button_register.setOnClickListener {
performRegister()
}
}
// Register user credentials
private fun performRegister() {
val email = email_editText_register.text.toString()
val password = password_editText_register.text.toString()
// Firebase Authentication - WORKING
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
.addOnCompleteListener {
uploadImageToFirebaseStorage()
}
}
// Upload selected photo to Firebase Storage - WORKING
private fun uploadImageToFirebaseStorage() {
if(selectedPhotoUri == null) return
val filename = UUID.randomUUID().toString()
val ref = FirebaseStorage.getInstance().getReference("/images/$filename")
ref.putFile(selectedPhotoUri!!)
ref.downloadUrl.addOnSuccessListener {
saveUserToFirebaseDatabase(it.toString())
}
}
// Save user associated credentials to Firebase Database - NOT WORKING
private fun saveUserToFirebaseDatabase(profileImageURL: String) {
val uid = FirebaseAuth.getInstance().uid ?: ""
val ref = Firebase.database.getReference("/users/$uid")
val user = User(uid, username_editText_register.text.toString(), profileImageURL)
ref.setValue(user)
}
}
// User credentials to be saved
class User(val uid: String, val username: String, val profileImageURL: String)
The saveUserToFirebaseDatabase()
method does not get executed? I have checked logcat
after inserting onSuccessListener
and onFailureListener
statements, however no error or debug information is highlighted. The app. pushes the image to storage but the DB is not updated. It remains empty. The workflow is as:
- Authenticate user by email and password. - WORKING
- Save profile image in Storage. - WORKING
- Save profile identifier, username, and image in DB. - NOT WORKING
Firebase DB rules:
{
"rules": {
".read": "true",
".write": "true"
}
}
I tried changing the hierarchy but that did not work either:
val uid = FirebaseAuth.getInstance().uid ?: ""
val ref = Firebase.database.getReference("/users/$uid")
val user = User(uid, username_edittext_register.text.toString(), profileImageUrl)
ref.child("users").child(uid).setValue(user)
I have read the Firebase documentation and everything seems to be in place. I also disabled my firewall but that did not work either. Can someone point out the problem here? Thank you in advance.
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.