Elad Naeh
Elad Naeh

Reputation: 17

My firebase/database shows nothing

Firebase myFirebaseRef = new Firebase("https://my-riddle1.firebaseio.com/");
// step 5: write data
myFirebaseRef.child("aaa").setValue("aaa");
myFirebaseRef.child("message").setValue("The best Scores");
myFirebaseRef.child("BestScore").child("finalscore").child(name).setValue(score1);

I have this written in my code but when I look in the FireBase, there is nothing over there. Also I did all the steps for the FireBase to work, just like it was shown in their website.

What should I do? Thank.

Upvotes: 2

Views: 1262

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

A quick guess is that your database rejects the write operations. By default projects created on firebase.google.com require the user to be authenticated before they can access the database.

To validate this is indeed what happens, pass a completion listener in to setValue():

myFirebaseRef.child("aaa").setValue("aaa", new Firebase.CompletionListener() {
    @Override
    public void onComplete(FirebaseError firebaseError, Firebase firebase) {
        if (firebaseError != null) {
            System.out.println("Data could not be saved. " + firebaseError.getMessage());
        } else {
            System.out.println("Data saved successfully.");
        }
    }
});

If indeed the write is rejected because of your security rules, you have a few options to fix/work around the problem. First you can enable public access to your database. This is not recommended for production databases, but typically acceptable in development. Alternatively (and the best option for production databases) you can sign the user in before trying to write the database.

Allow public access

You can change the security rules for you database to allow public access. See the first note in blue on this page in the Firebase documentation:

Note: By default, read and write access to your database is restricted so only authenticated users can read or write data. To get started without setting up Authentication, you can configure your rules for public access. This does make your database open to anyone, even people not using your app, so be sure to restrict your database again when you set up authentication.

Sign in before writing

You also using the Firebase 2.x SDK to write the data. For projects created on firebase.google.com, it is recommended to use the newer SDKs. With those you can easily sign in the user anonymously and then set the values:

final FirebaseDatabase db = FirebaseDatabase.getInstance();
final FirebaseAuth auth = FirebaseAuth.getInstance();
auth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        FirebaseUser user = firebaseAuth.getCurrentUser();
        if (user != null) {
            // User is signed in, write to the database
            db.getReference().child("aaa").setValue("aaa");
        } else {
            // User is not signed in, sign them in now
            auth.signInAnonymously();
        }
    }
});

This is just a quick code snippet to get you started. I highly recommend that you follow the latest Firebase documentation for Android and take the Android codelab too.

Upvotes: 1

Related Questions