Identify your first issue
In this section, we’ll walk through identifying and diagnosing an issue that blocks the main thread, resulting in an ANR (App Not Responding) event.
Issue detected: ViewModel blocking the screen on the main thread
When the main UI thread is blocked, the app can slow down or freeze. This is often triggered by time-intensive operations running on the main thread, which can lead to ANR events.
In this scenario, we will:
- Simulate a 1-second delay in the
MainActivityViewModel
of the NowInAndroid app - Track user interactions to observe the impact on app performance.
- Review timeline data on the Kotzilla Platform to understand the delay’s effects.
1.- Simulate a 1-second delay in MainActivityViewModel
To replicate a main-thread delay scenario, we’ll introduce a 1-second delay in the MainActivityViewModel
. This simulated delay lets us observe the effects of blocking operations on the app’s performance, specifically in the main thread.
Code example: simulating a delay in MainActivityViewModel
class MainActivityViewModel(
userDataRepository: UserDataRepository,
) : ViewModel() {
init {
// Introduce a 1-second delay to simulate a main-thread block
Thread.sleep(1000)
}
}
2. User actions to generate the user session data
To create a new user session for the NowInAndroid app and observe the effects of the simulated delay, follow these steps in Android Studio:
- Launch the App
- Open Android Studio.
- From the menu, click on Run > Run 'app' (or use the shortcut Shift + F10).
- This action will build and deploy the app to your connected device or emulator, starting a new user session.
- Interact with the App
- Scroll down to the third article in the “For You” tab.
- When the article loads, click Back to return to the “For You” tab.
- Select the Saved tab.
- Navigate to the Interest tab and click on Accessibility.
- Stop the user session
- After performing the interactions, return to Android Studio.
- Click on the Stop button (red square icon) in the toolbar or go to Run > Stop 'app' to end the user session.
3.- Observing the issue on the Kotzilla Platform
After completing these actions, log in to the Kotzilla Platform click on the Dashboard button next to your app to access the app Dashboard View. Then click on the Sessions View to visualise the list of recorded user sessions. You should see the latest session, as shown in the example screenshot below.
- In the Max Event Duration column of the Session View, observe a 1-second delay. This metric highlights how long the main thread was blocked
-
By exploring the Timeline view, you can see how the 1-second delay affects the app’s startup flow, creating a visible pause between the "started" and "resumed" states of
MainActivity
. This gap reveals a period where the app appears unresponsive to the user: -
In the Threads view, notice the delay in the initialization of the
MainActivityViewModel
component.
-
A click on this resolution shows you the detailed sequence of events in the timeline in which we can also see the impact of this delay during the loading of the
MainActivityViewModel
:Takeway
A ViewModel that takes a long time to load can significantly impact your app’s responsiveness and overall user experience. The Kotzilla Platform shows you exactly which component is causing the problem, while also highlighting its impact on screen loading and multi-threaded execution