为什么我的Firebase实时数据库仅保存1个用户

时间:2018-06-18 10:51:06

标签: android database firebase authentication

为什么我的实时数据库没有添加新数据,而是仅替换新数据。必须添加不替换。

这是我添加新用户的代码:

public void addNewUser(String email, String username, String bio, String website, String interests, String address, String profile_photo){

    User user = new User(userID,1 , email, address, StringManipulation.condenseUsername(username));

    myRef.child(mContext.getString(R.string.dbname_users))
            .child(userID)
            .setValue(user);

    UserAccountSettings settings = new UserAccountSettings(
            interests,
            username,
            0,
            0,
            0,
            "",
            username,
            website,
            bio
    );

    myRef.child(mContext.getString(R.string.dbname_user_account_settings))
            .child(userID)
            .setValue(settings);
}

User类:

public class User {

private String user_id;
private long phone_number;
private String email;
private String address;
private String username;

public User(String user_id, long phone_number, String email, String address, String username) {
    this.user_id = user_id;
    this.phone_number = phone_number;
    this.email = email;
    this.address = address;
    this.username = username;
}

public User() {

}

public String getUser_id() {
    return user_id;
}

public void setUser_id(String user_id) {
    this.user_id = user_id;
}

public long getPhone_number() {
    return phone_number;
}

public void setPhone_number(long phone_number) {
    this.phone_number = phone_number;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

@Override
public String toString() {
    return "User{" +
            "user_id='" + user_id + '\'' +
            ", phone_number=" + phone_number +
            ", email='" + email + '\'' +
            ", address='" + address + '\'' +
            ", username='" + username + '\'' +
            '}';
}

UserAccountSettings类:

public class UserAccountSettings {

private String interests;
private String display_name;
private long followers;
private long truefans;
private long trustworthy;
private String profile_photo;
private String username;
private String website;
private String bio;

public UserAccountSettings(String interests, String display_name, long followers, long truefans, long trustworthy, String profile_photo, String username, String website, String bio) {
    this.interests = interests;
    this.display_name = display_name;
    this.followers = followers;
    this.truefans = truefans;
    this.trustworthy = trustworthy;
    this.profile_photo = profile_photo;
    this.username = username;
    this.website = website;
    this.bio = bio;
}

public UserAccountSettings() {

}

public String getInterests() {
    return interests;
}

public void setInterests(String interests) {
    this.interests = interests;
}

public String getDisplay_name() {
    return display_name;
}

public void setDisplay_name(String display_name) {
    this.display_name = display_name;
}

public long getFollowers() {
    return followers;
}

public void setFollowers(long followers) {
    this.followers = followers;
}

public long getTruefans() {
    return truefans;
}

public void setTruefans(long truefans) {
    this.truefans = truefans;
}

public long getTrustworthy() {
    return trustworthy;
}

public void setTrustworthy(long trustworthy) {
    this.trustworthy = trustworthy;
}

public String getProfile_photo() {
    return profile_photo;
}

public void setProfile_photo(String profile_photo) {
    this.profile_photo = profile_photo;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getWebsite() {
    return website;
}

public void setWebsite(String website) {
    this.website = website;
}

public String getBio() {
    return bio;
}

public void setBio(String bio) {
    this.bio = bio;
}

@Override
public String toString() {
    return "UserAccountSettings{" +
            "interests='" + interests + '\'' +
            ", display_name='" + display_name + '\'' +
            ", followers=" + followers +
            ", truefans=" + truefans +
            ", trustworthy=" + trustworthy +
            ", profile_photo='" + profile_photo + '\'' +
            ", username='" + username + '\'' +
            ", website='" + website + '\'' +
            ", bio='" + bio + '\'' +
            '}';
}

这就是我称之为firebaseMethod.addNewUser的方法:

 mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();

            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());

                myRef.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        // 1st check: Make sure the username is not already in use
                        if (firebaseMethods.checkIfUsernameExists(username, dataSnapshot)){
                            append = myRef.push().getKey().substring(3,10);
                            Log.d(TAG, "onDataChange: username already exists. Appending random string to name: " + append);
                        }

                        username = username + append;

                        // add new user to the database

                        firebaseMethods.addNewUser(email,username,"","","","","");

                        Toast.makeText(mContext, "Signup successful. Sending verification email.", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

                finish();

            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
            // ...
        }
    };

这是mAauth.signOut中的HomeActivity.class

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Log.d(TAG, "onCreate: starting.");

    setupFirebaseAuth();

    initImageLoader();
    setupBottomNavigationView();
    setupViewPager();

    FirebaseAuth.getInstance().signOut();


}

经过身份验证的用户的图片: enter image description here

数据库图片: enter image description here

  

很抱歉没有张贴图片,只是没有足够的代表。谢谢

1 个答案:

答案 0 :(得分:0)

Firebase允许在firebase实时数据库中存储多个用户的信息,但仅限于经过身份验证的用户。我想你还没有从你的第一个用户退出。这就是为什么,它只保存一个用户。你有没有创建一个注销按钮,你做过这样的事情:

FirebaseAuth mAuth = FirebaseAuth.getInstance();
mAuth.signOut();