Fix ANR caused by operationRepo.enqueue while loading is in progress #2233
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
One Line Summary
This PR will fix ANR caused by model store insertion while model store loading is stuck with some prolonged process.
Details
Motivation
Multiple users have reported that ANRs related to OneSignal have shown up in their analytics. We want to eliminate these ANRs to ensure smooth app start up.
Scope
This PR introduces a new object OSPrimaryCoroutineScope that can be used to run any task in a designated coroutine scope. We can assign a certain tasks that can be potentially blocked to run under this primary coroutine scope instead of the main thread so we don't block the main thread if the task can be blocked by other processes like loading from the disk. The PR specifically changes the enqueue calls in SessionListener and UserRefreshService because they can be called immediately after app startup.
Others
This PR contains 4 commits.
Testing
Unit testing
I have included a test unit to simulate a model insertion while loading is blocked by a lengthy process.
Manual testing
The first commit manually force the loading of operationRepo to take at least 5 seconds in order to simulate the issue. By checking out the first commit and run the demo app, we can observe an ANR on app startup. Once we check out the third commit which contains the fix, we can no longer reproduce the ANR.
Affected code checklist
Checklist
Overview
Testing
Final pass
This change is