Preloading using handler in Android non rxjava environment

During Android client interface development, we often need to display the data obtained from the server to the page layout. Since the precondition for data display to the layout is that the page layout has been initialized, otherwise null pointer exceptions will occur. Therefore, generally, we need to put the network request after the layout initialization is completed. The traditional page loading process is:

Question:

If the loaded UI layout is complex, or the initialization logic takes a long time to execute, the execution time of the network request is relatively late, and the time to finally complete the page loading is relatively long. If page initialization and network loading can be carried out at the same time, after both are executed, the network data can be displayed on the layout, so that we can shorten the loading time of the whole page. Therefore, the expected page loading process is:

This process is called preloading

The target task of preloading can be a network request or some other time-consuming operations. For example, load a picture to the control for display. Before implementing the preloading scheme, we need to understand the concept of syncbarrier in the handler working mechanism. For an overview of the barrier, see the introduction to "synchronous split bar" in this article, Here we simply understand it as:

Add a special msg to the messagequeue and take this msg as a tag. Before this tag is removed, other (non async) messages in the current messagequeue queue will not be processed by the handler.

We can ignore what is non async message first. If you need to know more, there is also a relevant introduction to "synchronous split bar" in this article.

With this feature, we can:

Start a handlerthread to asynchronously execute the network request. Set a syncbarrier flag. After the message will not be executed in the messagequeue. After the network request is successful, post a task to execute the display data layout. After the initialization is successful, remove the syncbarrier and post the task of displaying data to the UI thread to execute steps 3 and 4. The sequence can be exchanged

Among them, the setting flag syncbarrier can be set by Android API 22 and before

HandlerThread.getLooper().postSyncBarrier();

After Android API 23, the methods to be called are:

HandlerThread.getLooper().getQueue().postSyncBarrier();

Similarly, the methods of removing marks are as follows:

Unfortunately, these methods are @ hide and cannot be called directly. Fortunately, we still have reflection

The encapsulation tool class is as follows: preloader.java

Use instances in an activity:

By preloading, some asynchronous tasks can be executed in advance, which can be used to improve the overall speed. All of the above take the network request as the purpose of preloading. At the same time, it can also be used to preload pictures, preload files, read databases, etc; In addition to preloading, it can also be used as multiple tasks in parallel. After all tasks are executed, another task can be executed to process the execution results of all previous tasks.

Summary: during multithreading development, the following situations are often encountered:

Task a and Task B (or even more tasks) are the necessary and sufficient conditions for task C. in order to improve efficiency, we want AB to execute at the same time. When AB is completed, start executing task C. In this case, we can solve it in this way.

Finally, the source code download address

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>