2

I have a situation where I use this Library greenrobot/EventBus to save data and pass them on different activities. In this case i use EventBus to pass "order" and "cartItems" OBJECT CustomModel from some activities in a joint activity.

In this activity I have a method that needs values that are distributed in these two objects but these object call in different events like below. I have tried to call this method updateUI() in both events but always one of the objects is NULL.

It is possible to have an event when all objects have been setup?

Any detailed explanation about how events lifecycle works in EventBus is welcomed!

    @Subscribe(sticky = true)
    public void onOrderEvent(Order order) {
        this.order = order;
        updateUI();
    }


    @Subscribe(sticky = true)
    public void onBasketProductsEvent(Products products) {
        this.basketProducts = products;
        updateUI();
    }

    private void updateUI() {
        double subtotal = getSubTotalPrice(basketProducts.getProducts());
        double taxPrice = getTaxPrice(subtotal,order.getTax());
    }

When I call this method in both events I have some NullPointerException because always one of object is null.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96

2 Answers2

0

When I call this method in both events I have some NullPointerException because always one of object is null.

It's expected behaviour. It's because either this.order or this.basketProducts is not yet initialized.

You need to remember that a subscriber is always be called whenever you're posting an Event. For example, when you're calling the following:

EventBus.getDefault().postSticky(new Order());

then, the onOrderEvent(Order order) subscriber will be called immediately. The following are happened within the above case:

  • Event for Order is posted
  • Subscriber onOrderEvent(Order order)
  • this.order is initialized
  • updateUI is called without this.basketProducts initialized yet
  • NullPointerException is raised.

A simple fix can be done by checking if both the this.orderorthis.basketProductsare already initialized before calling theupdateUI`. Something like this:

private void updateUI() {

    // don't do update when both the required values is null.
    if(order == null || basketProducts == null) {
      return;
    }

    double subtotal = getSubTotalPrice(basketProducts.getProducts());
    double taxPrice = getTaxPrice(subtotal,order.getTax());
}

I think the reason that make you slightly confused with the EventBus mechanism is the sticky flag. Take a look http://greenrobot.org/eventbus/documentation/configuration/sticky-events/ for details about it.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
0

Although I tried to check if the mandatory values are null like below:

    private void updateUI() {

    if(order == null || basketProducts == null) {
      return;
    }

    double subtotal = getSubTotalPrice(basketProducts.getProducts());
    double taxPrice = getTaxPrice(subtotal,order.getTax());
}

This method never executed. I do not know why this happened, maybe they are being at the same time before variables are initialized. If anyone knows why this happens, feel free to leave a comment.

Anyway I found a solution for this problem.

We can get sticky event manually in the onCreate(Bundle savedInstanceState) Method and this would make all the values initialized at the same time and we can call method we want after values initialized like below:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //get basketProducts sticky Event manually
        basketProducts = EventBus.getDefault().getStickyEvent(Products.class);

        //get order sticky Event manually
        order = EventBus.getDefault().getStickyEvent(Order.class);

        if(order != null && basketProducts != null) {
           updateUI();
        }

    }