Tom Riddle
Tom Riddle

Reputation: 33

onChange method not called with firebase database access

I have a problem in doing a query and reading my firebase database via Java. I'm triyng to save the email of the logged user, but it seems that onChangeMethod is never called. I've cheched database rules and the sha1 fingerprint in firebase. Here's the code:

import android.util.Log;

import androidx.annotation.NonNull;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class LockerDatabase implements ValueEventListener{
    private FirebaseDatabase database;
    private DatabaseReference reference;

    private User currUser;
    private ArrayList<Password> passwords;

    public int count;

    public LockerDatabase(String email, String name) {
        database = FirebaseDatabase.getInstance();
        reference = database.getReference();

        currUser = new User();
        passwords = new ArrayList<>();

        if (!exists(email))
            createUser(email, name);
        else
            loadPasswords();
    }

    private void loadPasswords() {
        //TODO: read from db all passwords
    }

    public String getEmail(){
        return currUser.getEmail();
    }

    private void createUser(String email, String name) {
        currUser.setEmail(email);
        currUser.setName(name);
        currUser.setHashId(Integer.toString(email.hashCode()));

        //TODO: add user to database
    }

    private boolean exists(String email) {
        Query existence = reference.child("users").child(Integer.toString(email.hashCode()));
        existence.addValueEventListener(this);

        if (currUser == null)
            return false;
        else
            return true;
    }

    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        currUser.setEmail((String)dataSnapshot.child("email").getValue());
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.w("CAN", "user_cancelled", databaseError.toException());
    }

I'm trying to display the email in an activity, where it appears mi default value and not the correct one:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.locker_layout);

    Intent intent = getIntent();
    userEmail = intent.getStringExtra("email");
    userName = intent.getStringExtra("name");

    db = new LockerDatabase(userEmail, userName);

    TextView t = findViewById(R.id.textView);
    t.setText(db.getEmail());

    //TODO: display db results
    //TODO: if the user is new, set an access key
}

Here's the database: JSON here

I don't understand if my problem is due to the response time, or to some error in the code. Thank you!

Upvotes: 1

Views: 458

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

Dont use Integer.toString(email.hashCode()) as a child, either use the push() method that will generate a random key or use the user uid if you are using firebase authentication.

If you use push(), then store the key first:

String key  = reference.child("users").push().getKey();

Then when you want to retrieve data, do the following:

Query existence = reference.child("users").child(key);
existence.addValueEventListener(this);

Upvotes: 1

Related Questions