BusProvider Not Returning to @Subscribe

395 views Asked by At

I am using BusProvider with on Android to handle post backs to the main thread after executing an AsynTask. When the Async task is finished and it is successful it registers the Post and returns to the MAIN thread to display an alert dialog. When it fails though, i would like it to return to the same method in the MAIN thread and display a message as to why it failed. In this case it is signing up, so it could be as simple as "This email is already being used".

This doesnt seem to be working for me and it never makes it back to the MAIN thread.

In my Async task using the BusProvider, i am posting back this on failure:

 @Override
        public void failure(RetrofitError error) {
            BusProvider.getInstance().post(new SignupTask.ErrorEvent(new String(((TypedByteArray) error.getResponse().getBody()).getBytes())));
        }

It is then passed into the class ErrorEvent:

public class ErrorEvent {

    public String message;

    public ErrorEvent(String message) {
        this.message = ResponseParser(message);
    }

    public String ResponseParser(String response){
        //Handle JSON parsing here of response, need message + the array stack to display to the user what is exactly wrong
        return response;
    }

}

Still some work to be done there but before i parse the info i would like to just pass the response back for now.

The response is NOT null and i have checked this while debugging. Once it gets back to the BusProvider.getInstance().post(...) it never returns to the MAIN thread and hit my @Subscribe

@Subscribe:

@Subscribe
public void onSignUpSuccess(SignupResponse event) {
    loading.dismiss();

    if(!BuildConfig.DEBUG_MODE) {
        Log.i(TAG, "!BuildConfig.DEBUG_MODE : " + AnswersAnalytics.SIGN_UP_PATIENT);
        Answers.getInstance().logCustom(new CustomEvent(AnswersAnalytics.SIGN_UP));
    }
    AlertDialog alertDialog = new AlertDialog.Builder(SignupActivity.this).create();
    alertDialog.setTitle("Thank you");
    alertDialog.setMessage(event.getMsg());
    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    killSignup();
                }
            });
    alertDialog.show();
}

I would like to use this same @Subscribe to handle the error and display it.

Any help is much greatly appreciated!

1

There are 1 answers

8
deathangel908 On BEST ANSWER

You can't post event from another thread to UI thread. You could use Handler for this.

private Handler mHandler = new Handler(Looper.getMainLooper()) {
    @Override
    public void handleMessage(Message msg) {
        synchronized (this) {
            BusProvider.get().post(msg.obj);
        }
    }
};

private void post(Object message) {
    Message msg = mHandler.obtainMessage(0);
    msg.obj = message;
    mHandler.sendMessage(msg);
}

@Override
public void failure(RetrofitError error) {
    this.post(new SignupTask.ErrorEvent(new String(((TypedByteArray) error.getResponse().getBody()).getBytes())));
}