Social network. Follow the functionality using the firebase database.

I am creating a social network like Instagram. I used to work on many social networks using mySQL. I am new to Firebase server.

I want to search for users by name and want to show the "Follow" button / further in the list. But I'm a little confused to run Android-based queries in a standard format. I do not want to run unnecessary loops. Below is the structure of my database for the following table.

Node is the name followand follower_idrefers to the user who is following the user, and the user to follow is called followed_id.

How to write a simple query using android firebase to show all users with a name starting (for example, "an"), and with a status that I already watch it or not.

FYI: I do not use the Firebase API for recreation.

+4
source share
1 answer

How to write a simple query using android firebase to show all users with a name starting (for example, "an"), and with a status that I already watch it or not.

I think you cannot do this in a single request.

You can try to do this 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 that this is not very simple and looks secretive. It may be slow, but worth a try.

, firebase. firebase, here.

0

Source: https://habr.com/ru/post/1669667/


All Articles