tajchert / BusWear

EventBus for Android Wear devices.
Apache License 2.0
257 stars 23 forks source link

Inside Services post / postRemote only post local #9

Open paulgavrikov opened 9 years ago

paulgavrikov commented 9 years ago

Code works in Activities, but inside a service both post or postRemote only post to the local event listeners.

public class WearService extends Service {
    public WearService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);
    }

    public void onEvent(Long i) {
        L.e("onEvent:long "+i);
        pushToWear();
    }

    public void onEvent(String s) {
        // called after pushToWear!!!!
    }

    private void pushToWear() {
     ...
        EventBus.getDefault().postRemote(payload.toString(), this);
    }
}
paulgavrikov commented 9 years ago

Looks like, post / postRemote work only on the UI Thread, so assign a handler in onCreate of the Service (this is executed on the UI Thread) and then post Runnables with postRemote to it.

tajchert commented 9 years ago

Thanks for explanation. I think this is EventBus issue/topic so If you feel the same please close issue as it is not directly BusWear related.

paulgavrikov commented 9 years ago

Well, I don’t know theoretically you could build a workaround inside Wearbus. But anyway I’d suggest you to write this down somewhere, nearly drove crazy to find out why it wasn’t working ;-)

On 10 Jun 2015, at 23:11, Michal Tajchert notifications@github.com wrote:

I think this is EventBus issue/topic so If you feel the same please close issue as it is not directly BusWear related.

— Reply to this email directly or view it on GitHub https://github.com/tajchert/BusWear/issues/9#issuecomment-110915698.

powder366 commented 9 years ago

Should not postRemote only post to remote? I have a case where it posts local, is this not a bug? Update: This problem was the same as "paulgavrikov" had.

paulgavrikov commented 9 years ago

It should but as you said it does not always work

Am 26 Aug 2015 um 23:53 schrieb powder366 notifications@github.com:

Should not postRemote only post to remote? I have a case where it posts local...

— Reply to this email directly or view it on GitHub.

tajchert commented 9 years ago

postRemote is not guaranteed as it uses Messaging API (to speed up event propagation) instead of Data API but doesn't guarantees delivery (if for example watch loses connection, or is out of range).

paulgavrikov commented 9 years ago

If it runs outside your Main Context it will go crazy. Check my issue in that

Am 27 Aug 2015 um 00:01 schrieb powder366 notifications@github.com:

Yes, but why does it end up local?

— Reply to this email directly or view it on GitHub.

powder366 commented 9 years ago

Im doing a postRemote within an onEvent and it seems that its treating my postRemote as already posting thus in postRemote "(!postingState.isPosting)", isPosting is "true", thus skipping it. So recursive posts is not possible, or?