0

I am using Firebase for the first time and I am attempting to read and write to the Realtime Database. However, when I completed Google's tutorial and ran my app the read/write method failed. Why would a simple read/write fail?

I made sure that the authentication was set to "allow read, write;". I checked my Gradle scripts as well and I didn't see anything missing at the project or the app level.

Code from tutorial:

private static final String TAG = "MainActivity";
...
writeReadDatabase()
...
public void writeReadDatabase()
    {
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("message");
        myRef.setValue("first write and read");

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String value = dataSnapshot.getValue(String.class);
                Log.d(TAG, "Value is: " + value);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.w(TAG, "Failed to read value.", databaseError.toException());
            }
        });
    }

Debug Logs:

W/SyncTree: Listen at /message failed: DatabaseError: Permission denied
W/RepoOperation: setValue at /message failed: DatabaseError: Permission denied

The database is just the default message saying the database is ready.

I'm trying to get the Logs to show up in Android Studio and for something to register in the database to see that it's working. I don't see a reason why permissions would be denied when no rules are set.

Edit:
*I was on Firestore.

Edit2: *My rules were set to false.

Edit3:

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

I changed the rules and ran the app once more, the database works now.

BarNull
  • 11
  • 8
  • The security rules you're showing apply to Cloud Firestore, while the code you're showing is accessing the Realtime Database. While both databases are part of Firebase, they're completely separate, and the security rules for one don't apply to the other. To fix the error, you will have to set the rules for the Realtime Database. For a walkthrough of how to do that, see https://stackoverflow.com/a/52129163 – Frank van Puffelen Apr 21 '19 at 21:07
  • Since you've discovered this yourself too, please update your question to remove the irrelevant Firestore rules, and include the minimum Realtime Database rules with which you can reproduce the problem. – Frank van Puffelen Apr 21 '19 at 21:08

0 Answers0