Firebase query doesn't seems to work in andorid

Aman Chatterjee :

I am facing a very wired problem in firebase database when I tried to query my user_name field to check if it already exists in the database or not. It was working earlier but it suddenly stopped working ... Here is my code :

 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) {

            }
        });

I don't know why but every time i run it . Its saying "username does't exist" even though it actually exist in the database.

 my firebase database

I don't know what to do. I think the problem started when I updated my firebase dependency to "16.0.5"

Here is my Data model of users class:

    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;
    }
}

UPDATE : I am getting this warning on my "user_name" field :

"Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding '".indexOn": "user_name"' at users to your security and Firebase Database rules for better performance"

Is this causing the issue?? How can i add indexOn

Problem Solved : I think ".indexOn" was the issue and also i was using firebase offline features and forgot to sync the particular node, i just did "nodeRefrence.keepSynced(true)" and now every thing is working..

Alex Mamo :

According to your edited question:

"Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding '".indexOn": "user_name"' at users to your security and Firebase Database rules for better performance"

It sounds like you may not have defined an index on the field you're querying on. If there is no index, the filtering cannot be done on the server. So in that case the server sends all data to the client, which does the filtering.

Is this causing the issue? How can i add indexOn?

If this is indeed the cause of your problem, you can fix it by adding an index in your rules file. Knowing that you are ordering users node on a property called user_name, you should use the following lines of code:

{
  "rules": {
    "users": {
      ".indexOn": "user_name"
    }
  }
}

For more information about this topic, I recommend you see the official documentation regarding indexing data in Firebase.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=130818&siteId=1