EventBus Post or postSticky from AbstractThreadedSyncAdapter to Activity

770 views Asked by At

I've googled for a while now, read the docs but I couldn't find any answer. My [simplified] scenario is:

The user first logs-in to the app, a sync request is fired, my onPerformSync gets called. I'm trying to post events through EventBus from AbstractThreadedSyncAdapter to my MainActivity. When the sync completes, I post an event, that should get called on MainActivity to sinalize it's all done, but it isn't working. My code:

AbstractThreadedSyncAdapter

 //Constructor
  public MyMoneySyncAdapter(Context context, boolean autoInitialize) {
         super(context, autoInitialize);
         this.context = context;
         EventBus.getDefault().post(new SyncEvent(SyncEvent.SyncStatus.Started));
     }

The onPerformSync:

EventBus.getDefault().postSticky(new SyncEvent(SyncEvent.SyncStatus.Running));
//...some ops....
//all done
EventBus.getDefault().postSticky(new SyncEvent(SyncEvent.SyncStatus.Finished));

And MainActivity:

@Override
    protected void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
        EventBus.getDefault().getStickyEvent(SyncEvent.class);
    }


public void onEventMainThread(SyncEvent event){
            //never gets called  
}

Is it even possible to post an event from the syncService? If not, what is the best solution for this problem ?

Thanks !

1

There are 1 answers

0
Leonardo On BEST ANSWER

I was using android:process:sync in my syncAdapter, just removing this worked with LocalBroadcastManager, but I think that with EventBus would work just fine. That's because when doing inter-process communication, you can't send a LocalBroadcast or an Event, it must be on the same process, otherwise you should use another approach.