In 's previous article , we mentioned that the modern WorkManager API has also improved tool support. In this article, we will combine actual cases to see what improvements are made. If you prefer to see this through video, check it out here:
https://www.bilibili.com/video/BV1tS4y1F7Bc/?aid=681676116&cid=513617482&page=1
△ Android Studio support for modern WorkManager
Starting with Android Studio Arctic Fox, you can inspect and debug directly within Android Studio using the Background Task .
The Background Task Inspector is part of the App Inspection suite and provides richer and more detailed information on several aspects of your app. To take full advantage of the inspector, your app needs to use WorkManager 2.5 or later.
App Inspector Enable Method
The quickest way to open the App Inspector for the first time is to press the "Shift" key twice in a row, then type App Inspection in the pop-up dialog and press Enter to confirm. You can also open the App Inspector panel by selecting from the menu bar: View → Tool Window → App Inspection.
△ Open the App Inspection panel
Example demo
This demo uses Android Studio Bumblebee and the WorkManager sample which is publicly available on GitHub.
Switch to the Background Task Inspector tab in the App Inspection panel, check the Filter in the application, and click the Apply button. It can be seen that as the filtering continues, the status of each Worker in its life cycle is also constantly changing. Since these Workers run sequentially, the CleanupWorker needs to finish before the next Worker starts. All other workers are either queued or blocked at this time.
△ Use the WorkManagerSample app and app inspector panel
Now that the CleanupWorker has completed, the other Workers in the queue will also run in order.
△ Graph view using the App Inspector panel
You can also view the contents of the entire connection queue from an image like the one shown. Please select a Worker and switch to the diagram view (the button is a flowchart style). For more complex queues, the diagram view is an efficient and indispensable means of understanding. From both views, you can click directly on any Worker for a more detailed analysis.
△ Details of background tasks
At the top is a basic description message, including the clickable class name and a unique ID.
Database Checker
△ Database Inspector Panel
We talked about the previous content, WorkManager will persist your work data, so let's use the Database Inspector (Database Inspector) to further see how this is achieved. You can click on the Database Inspector tab and look there for a job's unique ID. You'll find that every piece of information about that particular execution is saved.
Background Task Checker
You can also go further and see which queue a particular execution is scheduled to. Compared with manually searching for call information, this grouping method can significantly improve the analysis efficiency. You can see if a particular worker is throttled, as well as more detailed frequency and execution status information.
The WorkManager can consume the output data from one of the Workers and pass it to another Worker downstream in the pipeline. You can see this in the Results of the task details. The output data contains the unique identifier of each Worker and is delivered in queue order.
△ Demonstrate the scenario where Worker fails to execute
Here is a scenario for you when a worker in the work chain fails to execute. A faulty Worker is inserted into the job's continuation queue, and the application is restarted. As you can see, the results of the last run are still here for you to check. This further confirms the work data persistence feature just mentioned. Start and apply the filter here, select the Worker and click on the graph view again, so you can quickly locate where the work starts to go wrong (you can notice the exclamation mark here). From here you can click directly to the Worker view and continue debugging the session that went wrong.
This article has given you a brief introduction to the new background task inspector. That's all for WorkManager, for more resources, see:
- Modern WorkManager API Released
- WorkManager series: Dagger to customize WorkManager
- WorkManager Documentation: tasks with
- Codelab: Use WorkManager to handle background tasks
- Codelab: Advanced WorkManager Knowledge
- WorkManager sample code
You are welcome here to submit feedback to us, or share your favorite content and found problems. Your feedback is very important to us, thank you for your support!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。