简体   繁体   English

EventBus-事件生命周期有些麻烦

[英]EventBus - some trouble with event lifecycle

I have a situation where I use this Library greenrobot/EventBus to save data and pass them on different activities. 我遇到的情况是,我使用此库greenrobot / EventBus保存数据并将其传递给其他活动。 In this case i use EventBus to pass "order" and "cartItems" OBJECT CustomModel from some activities in a joint activity. 在这种情况下,我使用EventBus通过联合活动中的某些活动传递“ order”和“ cartItems” OBJECT CustomModel。

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. 我试图在两个事件中都调用此方法updateUI(),但是对象之一始终为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! 欢迎任何有关事件生命周期在EventBus中如何工作的详细解释!

    @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. 当我在两个事件中都调用此方法时,我会遇到一些NullPointerException,因为对象的始终为null。

When I call this method in both events I have some NullPointerException because always one of object is null. 当我在两个事件中都调用此方法时,我会遇到一些NullPointerException,因为对象的始终为null。

It's expected behaviour. 这是预期的行为。 It's because either this.order or this.basketProducts is not yet initialized. 这是因为this.orderthis.basketProducts尚未初始化。

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. 然后,将立即调用onOrderEvent(Order order)订户。 The following are happened within the above case: 在上述情况下发生了以下情况:

  • Event for Order is posted 订单活动已过帐
  • Subscriber onOrderEvent(Order order) 订阅者onOrderEvent(Order order)
  • this.order is initialized this.order被初始化
  • updateUI is called without this.basketProducts initialized yet 在未初始化this.basketProducts情况下this.basketProducts updateUI
  • NullPointerException is raised. 引发NullPointerException

A simple fix can be done by checking if both the this.order or this.basketProducts are already initialized before calling the updateUI`. 通过are already initialized before calling the updateUI` are already initialized before calling the检查this.order or this.basketProducts are already initialized before calling the可以完成一个简单的修复。 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. 我认为让您与EventBus机制有些混淆的原因是sticky标记。 Take a look http://greenrobot.org/eventbus/documentation/configuration/sticky-events/ for details about it. 请查看http://greenrobot.org/eventbus/documentation/configuration/sticky-events/了解有关它的详细信息。

Although I tried to check if the mandatory values are null like below: 虽然我尝试检查强制值是否为null,如下所示:

    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: 我们可以在OnCreate(束savedInstanceState)方法 手动得到粘事件 ,这将使得在同一时间初始化所有的值,我们可以调用方法,我们要象下面这样初始化值后:

    @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();
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM