Eventbus sticky events lag in the delivery of data. Transfer big data between activities

Encountered a large data collection of activity in the project. Use EventBus to lag behind traditional functions. The contents of the Ali development manual found on the Internet. Take notes.

The plan given by Ali is to transmit data through EventBus.

 Sticky events of EventBus

EventBus is actually used in many commercial projects. Here is a brief introduction on how to use EventBus sticky events to complete the transfer of data between activities.

EventBus is an Android-optimized Publish/subscribe message bus, which simplifies the communication between various components in the application, and between components and background threads.

To use EventBus in Activity, you need to call the register() and unregister() methods in pairs according to the life cycle of the Activity. Ordinary events will only occur after register(), and events that occurred before registration will not be received.

The Sticky Event of EventBus is used here to achieve this. EventBus maintains a Map object stickyEvents internally for caching sticky events.

Sticky events are sent using the postSticky() method, which caches the events in the stickyEvents Map object, so that when the next registration, the event will be taken out and thrown to the registered component. In this way, a sticky lagging event is sent and received.

Next, let's look at the usage details of EventBus sticky events.

1.  The difference between annotations

The annotations of sticky events are slightly different from the annotations of ordinary events, and threadMode and sticky parameters need to be added.

@Subscribe(threadMode = ThreadMode.MAIN,sticky = true)

publicvoidonStickyEvent(MyStickyEvent event){

//...

}

Note that these two additional parameters are necessary.

2.  The difference between calling methods

When sending a message, you need to use postSticky() to replace the post() method.

It should be noted that sticky events are cached using the Map structure, and the event object type is used as the Key to cache, so for the same type of data, it will only cache the last sent data.

3.  Pay attention to the cleanup incident

As mentioned earlier, sticky events are stored in a Map object. It does not actively clean up the objects stored in it. Developers need to clean up manually.

EventBus provides two types of methods, removeStickyEvent() and removeAllStickyEvents(), which are used to clean up fixed data and all data respectively.

We need to manually call these two methods at the right time to clean up sticky events. If you do not clean up sticky events, you will receive sticky events every time you register().

4.  EventBus sticky events

The sticky event itself is separated from the Android Intent data transfer mechanism. It is important to know that the Activity will be destroyed and rebuilt in some special circumstances. In this case, the data passed through the Intent can continue to be recovered from the Intent The data passed in the last page.

However, sticky events through EventBus may cause data loss during destruction and reconstruction.

If you want to use EventBus sticky events to transfer big data between pages, there are still many details that need to be adjusted according to the business.

Fourth, summary moment

Today we talked about the reason why TransactionTooLargeException is triggered by passing big data through Intent between Activities, and how to solve it, and finally I will briefly summarize.

  1. Intent cannot transfer large data because it uses Binder communication mechanism internally, and the Binder transaction buffer limits the size of the transferred data.
  2. The size of the Binder transaction buffer is limited to 1MB, but this size is shared, that is, it is not absolutely safe to transmit data below 1MB, depending on the current environment.
  3. Do not challenge the limit of the size of the data transmitted by the Intent. For large data, such as long strings, Bitmaps, etc., do not consider the scheme of data transmission by the Intent.
  4. To solve the problem of big data transmission, you can start from the data source, restore the data according to the identification of the data, or first persist and then restore. You can also use EventBus sticky events to solve.

Guess you like

Origin blog.csdn.net/qq_36355271/article/details/101264339