在我的应用程序中,用户登录。首先,我在邮件地址上验证用户。
protected Boolean doAuthenticateWithEmail(final String mEmail, String mPassword)
在onTuthenticated的回调方法中,用户使用以下POJO存储在Firebase数据库中:
public class User {
private String emailAddress;
private String profitNumber;
private String userName;
public User() {
//Default constructor used by Firebase
}
public User(String emailAddress, String profitNumber, String userName) {
this.emailAddress = emailAddress;
this.profitNumber = profitNumber;
this.userName = userName;
}
public String getEmailAddress() {
return emailAddress;
}
public String getProfitNumber() {
return profitNumber;
}
public String getUserName() {
return userName;
}}
这是将userobject存储在代表邮件地址的节点下的代码。
Firebase userRef = new Firebase(Constants.FIREBASE_URL_USERS);
userRef.setValue(user.getEmailAddress().replace(".",","));
Firebase mailRef = userRef.child(user.getEmailAddress().replace(".",","));
mailRef.setValue(user);
我的问题是: 如果此用户已存在,最佳做法是什么?我应该首先检查数据是否存在,或者只是覆盖现有数据。
答案 0 :(得分:0)
事实证明需要进行检查。
/**
* Encode user email replacing "." with "," to be able to use it
* as a Firebase db key
*/
String mEncodedEmail = mEmail.replace(".", ",");
/* If the user doesn't exists, make a user */
final Firebase userLocation = new Firebase(Constants.FIREBASE_URL_USERS).child(mEncodedEmail);
userLocation.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
/* If nothing is there ...*/
if (dataSnapshot.getValue() == null) {
userLocation.setValue(user);
}
}