简体   繁体   中英

How to get data from firebase realtime database and set it to Singleton class

have some problem with my splash activity. While user is loged in, the first screen that opening is Splash activity. In splash activity, I'm checking if the UserSingleton class is null (Like if(UserSingleton.getInstance==null)). And if it null, i want to set the data from Database (all data from UserDetails path) to the Singleton class (I need it because i will use this data for more times).

So what i have for now.

My splash activity:

private FirebaseAuth mAuth;
    private FirebaseDatabase db;
    private DatabaseReference users;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        mAuth = FirebaseAuth.getInstance();
        db = FirebaseDatabase.getInstance();
        users = db.getReference();

        loadUserDetailsData();


    }

    public void loadUserDetailsData() {
        if (UserSingleton.getInstance().getmUserName() == null) {
            ValueEventListener eventListener = new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot ds : dataSnapshot.getChildren()) {


                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                }
            };
            users.addListenerForSingleValueEvent(eventListener);

        }
    } 

UserSingleton.class

public class UserSingleton {

    private static UserSingleton instance = null;
    private String mUserName;
    private String mEmail;
    private String mPartnerName;
    private String mEventLocation;
    private String mEventDate;
    private String mPassword;

    public String getmPassword() {
        return mPassword;
    }

    public void setmPassword(String mPassword) {
        this.mPassword = mPassword;
    }

    private UserSingleton() {
        //Empty constructor
    }

    public static UserSingleton getInstance() {
        if (instance == null) {
            instance = new UserSingleton();
        }
        return instance;
    }

    public String getmUserName() {
        return mUserName;
    }

    public void setmUserName(String mUserName) {
        this.mUserName = mUserName;
    }

    public String getmEmail() {
        return mEmail;
    }

    public void setmEmail(String mEmail) {
        this.mEmail = mEmail;
    }

    public String getmPartnerName() {
        return mPartnerName;
    }

    public void setmPartnerName(String mPartnerName) {
        this.mPartnerName = mPartnerName;
    }

    public String getmEventLocation() {
        return mEventLocation;
    }

    public void setmEventLocation(String mEventLocation) {
        this.mEventLocation = mEventLocation;
    }

    public String getmEventDate() {
        return mEventDate;
    }

    public void setmEventDate(String mEventDate) {
        this.mEventDate = mEventDate;
    }

Database path and data: 在此处输入图像描述

Singleton doesn't work in this case, first to retrieve data using a model class in firebase, you need a public constructor not private. Second, singleton pattern is used if you want one object of that class, if you are going to retrieve data from the database you are going to be creating more than one object since you have multiple users. Check this:

https://www.geeksforgeeks.org/singleton-class-java/


Assuming you changed from singleton then apply the following to your code:

Then first change the constructor to become public Users(){} (that is if you changed the name of the class to Users ).

Then, to retrieve the data from the database, first change the database structure to the following:

Users
   userId
       mEmail : ...
       mEventDate: ...
       mEventLocation : ....
       mPartnerName : ...
       mUserName :   ....

So in your database structure remove the node UserDetails because its a bit useless since you can use the userId to identify this user. Also dont use password in the database it is already encrypted in the firebase authenication. If you want to add it to the database then encrpyt it before adding it.

Then change the code to the following:

    users = db.getReference("Users");
    public void loadUserDetailsData() {
            ValueEventListener eventListener = new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                       Users retrievedData = dataSnapshot.getValue(Users.class);
                       String email        = retrievedData.getmEmail();
                    }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                }
            };
        users.addListenerForSingleValueEvent(eventListener);
      }

Friends, I managed to do it the next way, but not sure that is the good way. The only reason I do this is because I want to bring in a certain amount of data when the app loads. If anyone knows a better way, I'd love to.

public void loadUserDetailsData() {
        if (UserSingleton.getInstance().getmUserName() == null) {
            ValueEventListener eventListener = new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    UserSingleton retrievedData = dataSnapshot.getValue(UserSingleton.class);
                    UserSingleton.getInstance().setmUserName(retrievedData.getmUserName());
                    UserSingleton.getInstance().setmEmail(retrievedData.getmEmail());
                    UserSingleton.getInstance().setmEventDate(retrievedData.getmEventDate());
                    UserSingleton.getInstance().setmEventLocation(retrievedData.getmEventLocation());
                    UserSingleton.getInstance().setmPartnerName(retrievedData.getmPartnerName());

                    Log.d("UserEmailIs " + "email  ", UserSingleton.getInstance().getmUserName());
                    Log.d("UserEmailIs " + "email  ", UserSingleton.getInstance().getmEmail());
                    Log.d("UserEmailIs " + "email  ", UserSingleton.getInstance().getmEventDate());
                    Log.d("UserEmailIs " + "email  ", UserSingleton.getInstance().getmEventLocation());
                    Log.d("UserEmailIs " + "email  ", UserSingleton.getInstance().getmPartnerName());
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                }
            };
            users.addListenerForSingleValueEvent(eventListener);
        }
    }

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM