Skip to content

Instantly share code, notes, and snippets.

@aaalaniz
Created June 10, 2014 05:22
Show Gist options
  • Save aaalaniz/f5c41a619634969a7b8b to your computer and use it in GitHub Desktop.
Save aaalaniz/f5c41a619634969a7b8b to your computer and use it in GitHub Desktop.
Otto Main Thread Bus
public class MainThreadBus extends Bus {
private final Bus mBus;
private final Handler mHandler = new Handler(Looper.getMainLooper());
public MainThreadBus(final Bus bus) {
if (bus == null) {
throw new NullPointerException("bus must not be null");
}
mBus = bus;
}
@Override public void register(Object obj) {
mBus.register(obj);
}
@Override public void unregister(Object obj) {
mBus.unregister(obj);
}
@Override public void post(final Object event) {
if (Looper.myLooper() == Looper.getMainLooper()) {
mBus.post(event);
} else {
mHandler.post(new Runnable() {
@Override public void run() {
mBus.post(event);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment