RevenueCat / purchases-android

Android in-app purchases and subscriptions made easy.
https://www.revenuecat.com/
MIT License
234 stars 44 forks source link

Attempt to fix ANRs by moving some tasks during configure to background #1772

Closed vegaro closed 3 hours ago

vegaro commented 6 days ago

Description

This PR addresses a significant number of ANRs (Application Not Responding errors) reported, such as in issue #1629.

Problem

The method IdentityManager.configure is currently running on the main thread and accessing SharedPreferences. Accessing SharedPreferences can be costly, especially if the XML file storing the preferences is large. This cost is particularly high during the initial accesses, as the XML file needs to be read and parsed, which can block the main thread and lead to ANRs.

Solution

Custom Dispatcher: This PR introduces a custom dispatcher to the IdentityManager, allowing configure to run on a background thread. By moving this operation off the main thread, we avoid blocking it with the potentially expensive SharedPreferences access. Main Thread Optimization: The remaining setup in PurchasesOrchestrator has been deferred to execute after configure completes, ensuring the main thread remains unblocked and improving overall app responsiveness.

Notes

While this fix should mitigate the reported ANRs, it's possible there are other underlying causes contributing to these issues. This PR serves as a good initial step towards improving performance.

codecov[bot] commented 9 hours ago

Codecov Report

Attention: Patch coverage is 97.87234% with 1 line in your changes missing coverage. Please review.

Project coverage is 83.31%. Comparing base (80781e4) to head (639329d). Report is 3 commits behind head on main.

Files Patch % Lines
.../com/revenuecat/purchases/PurchasesOrchestrator.kt 94.11% 0 Missing and 1 partial :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #1772 +/- ## ========================================== + Coverage 83.26% 83.31% +0.05% ========================================== Files 223 223 Lines 7617 7634 +17 Branches 1071 1072 +1 ========================================== + Hits 6342 6360 +18 + Misses 854 853 -1 Partials 421 421 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.