{
"Users" : {
"750bf295-1a99-4b0f-a072-cae87d25f53e" : {
"email" : "aa@aa.aa",
"hotel" : {
"Island" : {
"author" : "Jole",
"comfort" : 5.0,
"food" : 2.0,
"latit" : 65.05243844618913,
"longit" : -19.647914953529835,
"name" : "Island",
"rating" : 3.0,
"review" : "OK nije los",
"service" : 2.0
},
},
"password" : "aa",
"username" : "Jole"
},
"8b76cd9e-6738-412d-9e55-986b1fe967f9" : {
"email" : "oo@oo.oo",
"hotel" : {
"Bologna" : {
"author" : "oooo",
"comfort" : 3.0,
"food" : 3.5,
"latit" : 44.507442,
"longit" : 11.348376,
"name" : "Bologna",
"rating" : 3.3333332538604736,
"review" : "woooooooow",
"service" : 3.5
},
"Lodz" : {
"author" : "oooo",
"comfort" : 5.0,
"food" : 1.5,
"latit" : 51.759325127252225,
"longit" : 19.455432258546352,
"name" : "Lodz",
"rating" : 3.3333332538604736,
"review" : "losa hrana",
"service" : 3.5
}
},
"password" : "oooo",
"username" : "oooo"
}
首先,这是我的JSON。现在,问题是,当我注册新用户时,我只想在之前从未使用过用户名和电子邮件(或两者都已存在)时才允许注册。
我使用MVP模式做到了,所以当我点击注册按钮时,它检查用户名/电子邮件/密码字段的长度/类型是否合适,如果一切正常,则调用presenter的registerNewUser方法:
@Override
public void registerNewUser(String username, String email, String password) {
interactor.checkIfUserExists(username, email, password);
}
然后调用交互器的chekIfUserExists方法:
@Override
public void checkIfUserExists(final String username, final String email, final String password) {
Firebase userRef = new Firebase("https://josip-my-application.firebaseio.com/Users/");
userRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
if (email.equals(user.getEmail()) || username.equals(user.getUsername())) {
presenter.onUserAlreadyExists();
break;
} else {
presenter.onUserDoesNotExist(username, email, password);
}
}
}
当调用presenter的方法onUserAlreadyExists时,它只是设置错误消息“此用户名或电子邮件已被使用”...当调用presenter的方法onUserDoesNotExist时,它调用interactors方法tryToRegister():
@Override
public void tryToRegister(final String username, final String email, final String password) {
userRef.createUser(email, password, new Firebase.ValueResultHandler<Map<String, Object>>() {
@Override
public void onSuccess(Map<String, Object> stringObjectMap) {
String uid = stringObjectMap.get("uid").toString();
userRef = new Firebase("https://josip-my-application.firebaseio.com/Users/" + uid);
userRef.setValue(createUser(username, email, password));
presenter.onSuccess(email, username);
}
@Override
public void onError(FirebaseError firebaseError) {
presenter.onFailure();
}
});
}
现在,问题在于for循环:
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
if (email.equals(user.getEmail()) || username.equals(user.getUsername())) {
presenter.onUserAlreadyExists();
break;
} else {
presenter.onUserDoesNotExist(username, email, password);
}
如果我尝试使用现有的电子邮件和不同的用户名注册,它会打破循环并调用presenter的onUserAlreadyExists方法,并不断告诉我已经使用了用户名或电子邮件,并且没有创建新用户,但是当我尝试注册现有用户时用户名和不存在的电子邮件,它打破了循环,告诉我已经使用了用户名或电子邮件然后它创建了具有现有用户名和不存在的电子邮件的新用户...我在这里遗漏了什么?
答案 0 :(得分:0)
这是因为你的if / else语句将检查并触发每个用户名。
换句话说,如果新注册的数据库中没有FIRST用户的用户名或电子邮件,它仍然会创建一个新用户,然后转到数据库中的下一个用户,检查,重复,并继续,直到找到副本,然后说&#34;此用户名或电子邮件已被使用,&#34;然后才停下来。
因此,为了解决这个问题,在完成快照中的每个用户并确保没有匹配时,不应该调用presenter.onUserDoesNotExist(username, email, password);
。
顺便说一句,很好的文档和解释你的问题。