Home > OS >  I don't see any data in my Realtime Database in Firebase's console
I don't see any data in my Realtime Database in Firebase's console

Time:05-24

I'm trying to figure out how to use Firebase properly, so I made this simple code, saving a "game" object with code and password. I also made it show the game's key on the screen. running the code, it works and shows what looks like a key on the screen, but I can't find any of the data anywhere on my Firebase Console. Here's the code:

public class testthing extends AppCompatActivity implements View.OnClickListener {
    EditText pass;
    TextView showkey;
    EditText code;
    Button create;
    String codestr;
    String passstr;
    FirebaseDatabase firebaseDatabase;
    DatabaseReference gameRef;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testthing);
        pass = findViewById(R.id.pass);
        code = findViewById(R.id.code);
        create = findViewById(R.id.create);
        showkey = findViewById(R.id.showthingy);
        firebaseDatabase = FirebaseDatabase.getInstance();
        create.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if(view==create){
            codestr=code.getText().toString();
            passstr=pass.getText().toString();

            Game g = new Game(codestr,passstr,"");
            gameRef = firebaseDatabase.getReference("gameRooms").push();
            g.key = gameRef.getKey();
            gameRef.setValue(g);
            showkey.setText(g.key);

        }
    }
}

with the "game" class being:

@IgnoreExtraProperties
public class Game {
    public String key;
    public String code;
    public String password;
    public Game(){
    }
    public Game(String code, String password,String key){

        this.code = code;
        this.password = password;
        this.key = key;
    }

}

CodePudding user response:

When you're using the following line of code:

gameRef.setValue(g);

The data you're trying to write into the database may succeed or fail, but you'll never know that since you aren't attaching a listener to check that. So solve this, you have to attach a listener as in the following lines of code:

gameRef.setValue(g).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if (task.isSuccessful()) {
            Log.d("TAG", "The operation is complete.");
            showkey.setText(g.key);
        } else {
            Log.d("TAG", task.getException().getMessage());
        }
    }
});

If the operation fails, most likely the Firebase servers rejected your operation. So make sure you have the proper rules. Otherwise, set the key to the TextView. Remember that all Firebase APIs are asynchronous. This includes the write and the read operations. If you need later to read the data, I recommend you check the following article:

  • Related