Pick the fix
Play Console shows ANRs whose main thread is waiting on synchronized(cache) while a worker thread holds it during a network call. Best fix?
Hint 1 of 1
Why is the main thread waiting?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Don't hold the lock during I/O: fetch first, then lock only to update the cache
Correct. Keep critical sections tiny and never do I/O inside them.
Why not the others?
- B Increase the ANR timeout. Apps can't change it.
- C Move the main-thread read to onResume. Still blocks on the lock.
- D Catch the ANR. It isn't an exception you can catch.
Illustrative, based on Compose and Android rules; not a device recording
Text version
| Frame | Work (ms) | Result |
|---|---|---|
| F1 | 8 | on time |
| F2 | 7 | on time |
| F3 | 9 | on time |
| F4 | 8 | on time |
| F5 | 6 | on time |
| F6 | 7 | on time |
0 dropped frames at 60 Hz (16.7 ms budget); ANR dialog at 5 s: no.
| Frame | Work (ms) | Result |
|---|---|---|
| F1 | 8 | on time |
| F2 | 120 | ⏱ late (the next 7 dropped) |
| F3 | – | ✕ dropped (F2 still running) |
| F4 | – | ✕ dropped (F2 still running) |
| F5 | – | ✕ dropped (F2 still running) |
| F6 | – | ✕ dropped (F2 still running) |
| F7–F9 | – | ✕ dropped ×3 (F2 still running) |
7 dropped frames at 60 Hz (16.7 ms budget); ANR dialog at 5 s: no.
| Frame | Work (ms) | Result |
|---|---|---|
| F1 | 8 | on time |
| F2 | 5000 | ⏱ late (the next 299 dropped) |
| F3 | – | ✕ dropped (F2 still running) |
| F4 | – | ✕ dropped (F2 still running) |
| F5 | – | ✕ dropped (F2 still running) |
| F6 | – | ✕ dropped (F2 still running) |
| F7–F301 | – | ✕ dropped ×295 (F2 still running) |
299 dropped frames at 60 Hz (16.7 ms budget); main thread blocked for 5 s; ANR dialog at 5 s: yes.