question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

It's terrible to define lots of Custom events when post and subcribe

See original GitHub issue

I am Confused whether make use of eventbus to reconstruction my code, cause eventbus must define lots of Custom events,each action should have relevant event.

I think it’s terrible。

what i am think is changing post(object event), post(Event event), most of time ,I just post a Event, an it’s filterSet act as a FILTER that who can accept this Event。

that means the Event know that I will not serve others,I just serve filterSet declared.

filterSet may be { “com.test.demoActivity.class”,“com.test.demoActivity2.class”}

SO, my question is :What Problems of this design

public class Event{ public Set<String> filterSet; public Object msg; }

private void postToSubscription(Subscription subscription, Event event, boolean isMainThread) {
   //add filter condition
    if(!event.filter.isEmpty() && !event.filter.contains(subscription.subscriber.getClass())){
        return ;
    }
    switch (subscription.subscriberMethod.threadMode) {
    case PostThread:
        invokeSubscriber(subscription, event);
        break;
    case MainThread:
        if (isMainThread) {
            invokeSubscriber(subscription, event);
        } else {
            mainThreadPoster.enqueue(subscription, event);
        }
        break;
    case BackgroundThread:
        if (isMainThread) {
            backgroundPoster.enqueue(subscription, event);
        } else {
            invokeSubscriber(subscription, event);
        }
        break;
    case Async:
        asyncPoster.enqueue(subscription, event);
        break;
    default:
        throw new IllegalStateException("Unknown thread mode: " + subscription.subscriberMethod.threadMode);
    }
}

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
gavingccommented, May 10, 2014

Oh ouch, posts msdn reference…

IMHO what I was most worried about with an enum/type/filter pattern (I also tried it) was that too many registered listeners would fire and interrupt things unnecessarily. Until they each figured out that the message was not for them. Hence the anit-pattern.

The routing tools you have with EventBus are:

  1. Named buses (the default is one, you can create another, say just for db events.) I haven’t found this necessary so far. and
  2. Event classes.

Not sure if best practice but this is what I have found myself doing:

package com.example.myapp.db
public class  RequestEvent {
    public static class PreviousLogEntry { }
    public static class NextLogEntry { }
}
package com.example.myapp.db
public class  ResultEvent {
    // posted sticky.
    public static class CurrentLogEntry {
        public LogEntry logEntry;

        public CurrentLogEntry(LogEntry logEntry) {
            this.logEntry = logEntry;
        }
    }
    public static class LogEntryCount {
        public long count;

        public LogEntryCount(long count) {
            this.count = count;
        }
    }
}
public class MyFragment extends DialogFragment {
...
    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume.");

        Log.d(TAG, "Register (Sticky) EventBus.");
        EventBus.getDefault().registerSticky(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "onPause.");

        Log.d(TAG, "Unregister EventBus.");
        EventBus.getDefault().unregister(this);
    }
...
    /**
     * Current Log Entry change event handler.
     * @param resultEvent Event object passed in from EventBus.
     */
    public void onEventMainThread(ResultEvent.CurrentLogEntry resultEvent) {
        Log.d(TAG, "onEventMainThread ResultEvent.CurrentLogEntry");
        mLogEntry = resultEvent.logEntry;
        // optionally
        updateView();
    }
..
}
0reactions
411805662commented, May 11, 2014

@gavingc

yes ,I have thought about it. creating several buses according to logic module, but you should register several subscriber in one component(Activity),and it will make you puzzled when post Events, you must choose the right bus to send a post ,especially when someThing across Componnet ,across modules

and i have not understand what’s problem does your example code fixed

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dispatching custom events - The Modern JavaScript Tutorial
Custom events can be used to create “graphical components”. ... assign any properties into a regular new Event object after its creation.
Read more >
Is it poor form for a C# class to subscribe to its own published ...
After saying the above, normally there is no great harm in subscribing to your own event; in UI classes that represent forms it...
Read more >
Using custom events in React - LogRocket Blog
Learn how to build your own custom events in React apps, an essential skill for frontend devs of all levels, in this complete...
Read more >
Social Media Event Promotion: The Complete Guide
6 ways to promote an event on social media before it happens · 1. Post a countdown on Instagram Stories · 2. Create...
Read more >
Should You Put Several Event Types in the Same Kafka Topic?
In this context, I believe it's less important to define a topic as a grouping of messages with the same schema. Much more...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found