square / otto

An enhanced Guava-based event bus with emphasis on Android support.
http://square.github.io/otto/
Apache License 2.0
5.16k stars 847 forks source link

How to customize ThreadEnforcer #190

Closed chokhmah closed 8 years ago

chokhmah commented 8 years ago
screen shot 2016-08-12 at 12 07 13 pm

Thanks.

JakeWharton commented 8 years ago

The default behavior verifies that the main thread is used, so you can just call new Bus().

You can see the implementation of the main thread enforcer here: https://github.com/square/otto/blob/a85ae721d337d9129eb49f5dc90c8e65d49d48ca/otto/src/main/java/com/squareup/otto/ThreadEnforcer.java#L43-L50. A similar approach would be used for other threads as well.

chokhmah commented 8 years ago

Thanks.

My goal is to ensure that the Bus#post() will be run on main thread, because there are some method/class that calls Bus#post on a background thread. I used ThreadEnforcer.ANY and ThreadEnforcer.MAIN but it throws an exception when calling from a different thread. Also The bus I created is a singleton. My current implementation is I put Bus#post() inside a Handler#post() to make sure it is on the main thread. I am also thinking if I can customize the ThreadEnforcer to run the Bus#post on the main thread so that I won't use the Handler#post in handling the Bus#post. Does my current implementation correct? or am I overthinking the approach on ThreadEnforcer?

JakeWharton commented 8 years ago

Thread enforcer doesn't allow thread movement. Just validation.

On Fri, Aug 12, 2016, 12:18 AM chokhmah notifications@github.com wrote:

Thanks.

My goal is to ensure that the Bus#post() will be run on main thread, because there are some method/class that calls Bus#post on a background thread. I used ThreadEnforcer.ANY and ThreadEnforcer.MAIN but it throws an exception when then on calling is from different thread. Also The bus I created is a singleton. My current implementation is I put Bus#post() inside a Handler#post() to make sure it is on the main thread. I am also thinking if I can customize the ThreadEnforcer to run the Bus#post on the main thread so that I won't use the Handler#post in handling the Bus#post. Does my current implementation correct? or am I overthinking the approach on ThreadEnforcer?

— You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub https://github.com/square/otto/issues/190#issuecomment-239357655, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEVD2m7V_eQIRPX6DG1hIvzJY6T5Qks5qe_QFgaJpZM4Jiwo9 .

chokhmah commented 8 years ago

Okay. Thank you.