I'm trying to write into my Cloud Firestone the data regarding the registered user in collection Users, of course it must be associated with UID code in Authorization (The Registration/login system working correctly and write auths in my Firebase).
XML Register Button:
<Button
android:id="@+id/RegisterBtn"
android:layout_width="0dp"
android:layout_height="51dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="92dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="REGISTER"
app:backgroundTint="#870404"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Password" />
JAVA Register:
...
FirebaseFirestore database;
String userID;
...
database = FirebaseFirestore.getInstance();
mRegisterBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = mFullName.getText().toString().trim();
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
String confirmPassword = mconfirmPassword.getText().toString().trim();
...
//REGISTER THE USER
fAuth.createUserWithEmailAndPassword(email,password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
startActivity(new Intent(getApplicationContext(),MainActivity.class));
finish();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Register.this, "Error!" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
//FIRECLOUD INSERT DATA
//GET ID FROM AUTH
userID = fAuth.getCurrentUser().getUid();
saveOnFireCloud(userID,name,email);
}
});
...
private void saveOnFireCloud(String userID, String name, String email) {
Map<String,Object> mappa = new HashMap<>();
mappa.put("id",userID);
mappa.put("name",name);
mappa.put("email",email);
database.collection("Users").document(userID).set(mappa)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(Register.this, "Data Inserted!", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Register.this, "Error Inserting Data!" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
In Cloud Firestone I just created collection Users. I noticed that in Tool>Firebase>Firestone Cloud tell me: Connect your app to Firebase
but even if I already clicked on and seems built correctly, on bottom "Add Cloud Firestone to your App" button still appear instead of ✓
Did I developed the steps to link the UID in the correct way? When I click on register button,receive no errors in runtime, the Auth user is created but the Users data remains blank...I literally get mad, I'm just stuck on this step!
Thank you in advance for your answers!!
EDIT:
Thanks a lot for the answers,Frank.
Yes, if I set the breakpoint inside:
private void saveOnFireCloud(String userID, String name, String email)
:
this is the result at the line:
database.collection("Users").document(userID).set(mappa):
this = { Register@17322 }
userID = "YVI9zPC2VJbpd4JpMCeBOZNDsbD2"
name = "a" email = "test@gmail.com"
mappa = {HashMap@17356}
size = 3
database = {FirebaseFirestone@17357}
But if I set 2 breakpoints of
public void onComplete(@NonNull Task<Void> task) { and public void onFailure(@NonNull Exception e)
the debug skips over... and data is not witten..
I solved by replacing the method:
database.collection("Users").document(userID).set(mappa)
with the method:
database.collection("Users").add(mappa)
Seems that the .set(Hashmap) method can be used just to replace an existing object, instead the method .add(Hashmap) add the object to the database. I inserted correctly the record in the collection.
By the way, now I m stuck again on this problem, I tried to test it again and now, does not insert data in FirebaseCloud. When I click on continue,nothing happens! just in console "Frames are not available"
Resolved 2.0: It seems that Android studio has bugs with cash, to solve it, cancel AndroidStudio Folder in C:\Users\PIDIOTTO\AppData\Roaming\Google and Restart Android Studio! :)