Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Id Added To subscribers #682

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

muhammadzadeh
Copy link

Please consider the following scenario:
In Messenger, there is a list of chats that need to be updated. Therefore, this must happen by sending an event. The specified event is sent to all rows of chats by default. But we need to send the chat update event in only one row. In this case, the ID is used.
Eventbus-by-id

now when we use eventbus in list

and we need to send public and private events for specefic event, id is useful.
@muhammadzadeh muhammadzadeh changed the title Add id to subscribers Id Added To subscribers Nov 16, 2021
@andob
Copy link
Contributor

andob commented Nov 16, 2021

Instead of using IDs inside the library, why don't you create more descriptive event classes?

For instance, on your given example, say, you have a class describing a row with an user and its last message:

public class UserRowView
{
   ...
   @Subscribe
   public void onMessageReceived(String message)
   {
        lastMessageTextView.setText(message);
   }
   ...
}

You can reorganise the code like this:

class OnMessageReceivedEvent { text : String, receivedFromId : int }

public class UserRowView
{
   private int userId;
   //todo show user name, its last message, set userId
   ...
   @Subscribe
   public void onMessageReceived(OnMessageReceivedEvent event)
   {
        if (event.receivedFromId == userId)
        {
            lastMessageTextView.setText(message);
        }
   }
   ...
}

@muhammadzadeh
Copy link
Author

You are right. We just have to keep in mind that sending events to a large number of subscribers does not make sense when we are on the list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants