Skip to content

Use collectAsStateWithLifecycle instead of collectAsState in Compose#2488

Merged
skydoves merged 3 commits into
mainfrom
migrate/collectAsStateWithLifecycle
Jul 8, 2025
Merged

Use collectAsStateWithLifecycle instead of collectAsState in Compose#2488
skydoves merged 3 commits into
mainfrom
migrate/collectAsStateWithLifecycle

Conversation

@skydoves

@skydoves skydoves commented Jul 7, 2025

Copy link
Copy Markdown
Member

Use collectAsStateWithLifecycle instead of collectAsState in Compose.

Description

collectAsState is not lifecycle-aware and operates independently of Android's lifecycle system. This means it continues to collect data from the Flow even when the UI is no longer visible, potentially leading to unnecessary background work and performance issues.

On the other hand, collectAsStateWithLifecycle is a safer and more lifecycle-conscious alternative. It automatically suspends collection when the composable leaves the active lifecycle state (e.g., when the UI is not in the foreground), helping you avoid wasted resources and keeping your UI logic tightly aligned with the Android lifecycle.

Key advantages of collectAsStateWithLifecycle:

  • It respects the Lifecycle of the host activity or fragment.
  • Collection is automatically paused when the UI is in the background (e.g., during screen rotations or navigation).
  • It prevents memory leaks by ensuring the Flow does not keep running when it is no longer needed.

@skydoves skydoves requested a review from a team July 7, 2025 06:28
@skydoves skydoves self-assigned this Jul 7, 2025
@codecov

codecov Bot commented Jul 7, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 78.33%. Comparing base (1de5ad2) to head (67e2a89).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2488   +/-   ##
=======================================
  Coverage   78.33%   78.33%           
=======================================
  Files         286      286           
  Lines       10432    10432           
  Branches     1500     1500           
=======================================
  Hits         8172     8172           
  Misses       1614     1614           
  Partials      646      646           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tonidero tonidero left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! Thanks!

@skydoves skydoves added this pull request to the merge queue Jul 8, 2025
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Jul 8, 2025
@skydoves skydoves added this pull request to the merge queue Jul 8, 2025
Merged via the queue into main with commit 97dc872 Jul 8, 2025
14 checks passed
@skydoves skydoves deleted the migrate/collectAsStateWithLifecycle branch July 8, 2025 02:40
github-merge-queue Bot pushed a commit that referenced this pull request Jul 9, 2025
**This is an automatic release.**

## RevenueCat SDK
### ✨ New Features
* feat(purchases): Add setPostHogUserId() method to Purchases API
(#2495) via Hussain Mustafa (@hussain-mustafa990)
### 🐞 Bugfixes
* Improves button progress indicator size calculation. (#2485) via
JayShortway (@JayShortway)

### 🔄 Other Changes
* Revert "BC8 migration (#2477)" (#2501) via Toni Rico (@tonidero)
* Add codelab instructions on README file (#2489) via Jaewoong Eum
(@skydoves)
* Use collectAsStateWithLifecycle instead of collectAsState in Compose
(#2488) via Jaewoong Eum (@skydoves)
* Improve Composable stabilities (#2478) via Jaewoong Eum (@skydoves)
* [AUTOMATIC][Paywalls V2] Updates paywall-preview-resources submodule
(#2486) via RevenueCat Git Bot (@RCGitBot)
* BC8 migration (#2477) via Toni Rico (@tonidero)
* Fixes building sample apps with SNAPSHOT dependencies (#2483) via
JayShortway (@JayShortway)
* [AUTOMATIC][Paywalls V2] Updates paywall-preview-resources submodule
(#2484) via RevenueCat Git Bot (@RCGitBot)

Co-authored-by: revenuecat-ops <ops@revenuecat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants