0

I realize similar questions have been asked here, but previous solutions do not seem to apply. I'm developing an android app using Firebase auth, and I'm trying to keep track of the user handles that are registered by means of a Firebase database. This is very similar to Tanishq Sharma's answer to firebase duplicate username and custom sign-in.

My problem is that the onDataChange method of the ValueEventListener is never called. As suggested by Frank van Puffelen in Firebase with java (non-android) retrive information, I tried to add some wait time, but this did not solve the problem. After the wait time has passed, the onCancelled method is always called.

The code below shows the function that should return true if a user handle is listed in the Firebase database at node "takenHandles". I know that the connection to the database is working, because I'm able to add a new handle to the database by another part of my code without any issues. Any suggestions on what could be causing this problem?

EDIT It seems I don't have permission to comment on my own question. Thanks Frank, for your quick reply! That indeed explains why writing to the database works, because I do that just after a new user has been authenticated. I should probably consider changing the database rules.

public boolean handleTaken(final String mHandle) {
    final boolean[] isTaken = new boolean[]{true};
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference takenHandles = database.getReference("takenHandles");
    takenHandles.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.d(TAG, "ValueEventListener: onDataChange");
            isTaken[0] =  dataSnapshot.hasChild(mHandle);
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.d(TAG, "ValueEventListener: onCancelled");
            Toast.makeText(MyAccountActivity.this, "Connection error, please try again", Toast.LENGTH_SHORT).show();
        }
    });
    try {
        Thread.sleep(20000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return isTaken[0];
}
Community
  • 1
  • 1
Cuculus
  • 166
  • 1
  • 2
  • 10
  • 1
    If your `onCancelled()` gets called, you don't have permission to access the data that you're trying to read. You can confirm that by adding this code in `onCancelled()`: `throw databaseError.toException()`. Waiting (a piece of troubleshooting code I sometimes suggest to non-Android Java users) will not change the fact that you're not authorized. – Frank van Puffelen Sep 24 '16 at 20:59
  • The exception indeed read "Firebase Database error: Permission denied", so your answer solved my problem, thanks! – Cuculus Sep 25 '16 at 07:02

0 Answers0