当我尝试查询我的user_name字段以检查它是否已经存在于数据库中时,我在firebase数据库中遇到了一个非常有线的问题。它以前工作正常,但是突然停止工作了……这是我的代码:
Query query = myRef.child(getString(R.string.usersNode)).orderByChild("user_name").equalTo("codeni8");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
Log.d(TAG,"username already exist");
}else {
Log.d(TAG,"username does't exist");
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
我不知道为什么,但是每次我运行它时。它说“用户名不存在”,即使它确实存在于数据库中。
我不知道该怎么办。我认为问题开始了 当我将Firebase依赖关系更新为“ 16.0.5”时
这是我的用户类的数据模型:
public class Users {
private String user_id;
private String user_name;
private String display_name;
private String email;
private String profile_photo;
private String cover_photo;
private String bio;
private String website;
private long posts;
private long followers;
private long following;
private String phone_no;
private String gender;
public Users() {
}
public Users(String user_id, String user_name, String display_name, String email, String profile_photo, String cover_photo, String bio, String website, long posts, long followers, long following, String phone_no, String gender) {
this.user_id = user_id;
this.user_name = user_name;
this.display_name = display_name;
this.email = email;
this.profile_photo = profile_photo;
this.cover_photo = cover_photo;
this.bio = bio;
this.website = website;
this.posts = posts;
this.followers = followers;
this.following = following;
this.phone_no = phone_no;
this.gender = gender;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getDisplay_name() {
return display_name;
}
public void setDisplay_name(String display_name) {
this.display_name = display_name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getProfile_photo() {
return profile_photo;
}
public void setProfile_photo(String profile_photo) {
this.profile_photo = profile_photo;
}
public String getCover_photo() {
return cover_photo;
}
public void setCover_photo(String cover_photo) {
this.cover_photo = cover_photo;
}
public String getBio() {
return bio;
}
public void setBio(String bio) {
this.bio = bio;
}
public String getWebsite() { return website; }
public void setWebsite(String website) { this.website = website; }
public long getPosts() {
return posts;
}
public void setPosts(long posts) {
this.posts = posts;
}
public long getFollowers() {
return followers;
}
public void setFollowers(long followers) {
this.followers = followers;
}
public long getFollowing() {
return following;
}
public void setFollowing(long following) {
this.following = following;
}
public String getPhone_no() {
return phone_no;
}
public void setPhone_no(String phone_no) {
this.phone_no = phone_no;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
更新: 我在“用户名”字段上收到此警告:
“使用未指定的索引。您的数据将在客户端上下载并过滤。请考虑在用户的安全性和Firebase数据库规则中添加'“ .indexOn”:“ user_name”',以提高性能”
这是引起问题的原因吗?如何添加indexOn
已解决的问题: 我认为是“ .indexOn”的问题,而且我正在使用Firebase脱机功能并忘记同步特定节点,我只是做了“ nodeRefrence.keepSynced(true)”,现在一切正常。.
答案 0 :(得分:1)
根据您编辑的问题:
“使用未指定的索引。您的数据将在客户端上下载并过滤。请考虑在用户的安全性和Firebase数据库规则中添加'“ .indexOn”:“ user_name”',以提高性能”
听起来您可能尚未在要查询的字段上定义索引。如果没有索引,则无法在服务器上进行过滤。因此,在这种情况下,服务器会将所有数据发送到客户端,由客户端进行过滤。
这是引起问题的原因吗?如何添加indexOn?
如果这确实是造成问题的原因,则可以通过在规则文件中添加索引来解决此问题。知道您要在名为users
的属性上订购user_name
节点,您应该使用以下代码行:
{
"rules": {
"users": {
".indexOn": "user_name"
}
}
}
有关此主题的更多信息,建议您查看有关indexing data in Firebase的官方文档。
答案 1 :(得分:0)
以这种方式将ID传递给查询。
String userId = mFirebaseDatabase.push().getKey();
myRef.child(getString(R.string.usersNode)).child(userId).orderByChild("user_name").equalTo("codeni8");
尝试这种方法对我有用。
private void initView() {
mFirebaseInstance = FirebaseDatabase.getInstance();
mDatabase = mFirebaseInstance.getReference("usersDb/UserTable");
mRvData = findViewById(R.id.rvData);
mTvEmpty = findViewById(R.id.dlTvEmpty);
mRvData.setLayoutManager(new LinearLayoutManager(this));
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mUserList.clear();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
// here check id that available or not into db.
User user = dataSnapshot1.getValue(User.class);
mUserList.add(user);
}
allDataAdapter = new DisplayAllData(DisplayActivity.this, mUserList);
mRvData.setAdapter(allDataAdapter);
allDataAdapter.notifyDataSetChanged();
if (mUserList.isEmpty())
mTvEmpty.setVisibility(View.VISIBLE);
else
mTvEmpty.setVisibility(View.GONE);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
更多信息请参考此链接 https://www.androidhive.info/2016/10/android-working-with-firebase-realtime-database/