Nm Ann
Nm Ann

Reputation: 21

Connect users and Firebase Database

So I am keeping track of a users taps on the screen and I have the user sign up with their email. However, when it comes to using the Firebase Database I am lost. I am not sure, how to save integers and then it load up when the user logs in. Can someone point me in the right direction?

USER IMAGE DATABASE IMAGE

I have watched a ton of videos and none show how to store information with an integer.

Upvotes: 1

Views: 233

Answers (1)

Mohammed Rampurawala
Mohammed Rampurawala

Reputation: 3112

You don't need to do anything special for storing a integer or any other value in the Firebase DB.

You can use the uid of the current user and store the pojo/model directly into the the firebase DB.

For example, this can be your java model class:

 public class Model {
        private int tapCount=0;

        public int getTapCount() {
            return tapCount;
        }

        public void setTapCount(int tapCount) {
            this.tapCount = tapCount;
        }
    }

When you want to insert the tap count into the Firebase db. You need to take the previous tap counts and add it with the current count update/create the Model object and set it into the Firebase db in the uid.

FirebaseDatabase.getInstance().getReference()
    .child("user")
    .child(FirebaseAuth.getInstance()
    .getCurrentUser()
    .getUid())
    .setValue(model);

This code will insert the model under the user key and under user unique id.

Thanks,

Hope it helps.

Upvotes: 3

Related Questions