3

I am building a social network like Instagram. I have worked on many social networks before, using mySQL. I am new to firebase server.

I want to search users via name and want to show the follow/following button on the list view. But I am little confused to run android firebase queries in a standard format. I do not want to run unnecessary loops. Below is my database structure for the follow table.

Node name is follow and follower_id refers to the user who is following the user and user who gets followed is referred as followed_id.

How to write a simple query using android firebase to show all the users with the name starting (e.g "an") and with the status that I am already following him/her or not.

FYI: I am not using firebase rest API's.

Ankit Kamboj
  • 141
  • 2
  • 13

1 Answers1

0

How to write a simple query using android firebase to show all the users with the name starting (e.g "an") and with the status that I am already following him/her or not.

I think you can't do it in a single query.

You could try to do it with nested queries, something like this:

ChildEventListener childEventListener = new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {

        // Retrieve the userId
        User user = dataSnapshot.getValue(User.class);

        ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot usersFollowedDataSnapshot) {
                for ( DataSnapshot userFollowedDataSnapshot : usersFollowedDataSnapshot.getChildren() ) {

                    // Retrieve the follower structure
                    Follow follow = userFollowedDataSnapshot.getValue(Follow.class);

                    if ( myUserId == follow.getFollowerId() ) {
                        // This is a user that I'm following and which name starts with "am"
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        };

        Query secondQuery = mFirebaseDatabaseReference.child("follow").orderByChild("followedId").equalTo(user.getId());
        secondQuery.addListenerForSingleValueEvent(valueEventListener);
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
};


Query firstQuery = mFirebaseDatabaseReference.child("users").orderByChild("name").startAt("am").endAt("am\uf8ff");
firstQuery.addChildEventListener(childEventListener);

I know it's not very straightforward and looks disapponting. It could be also slow, but it worth trying.

Alternatively, you should consider to structure your database in a way to simplify the firebase queries. See more on firebase queries for example here and here.

Community
  • 1
  • 1
Valentino
  • 2,125
  • 1
  • 19
  • 26