collecting-flows-safely

Migrates Compose flow collection to lifecycle-aware APIs and hoists Flow parameters out of composables.

1|Updated Jul 6, 2026
One-click install
npx skills add https://github.com/citytexi/team-yg-pesonal-agent --skill collecting-flows-safely-citytexi
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: collecting-flows-safely
Source: https://github.com/citytexi/team-yg-pesonal-agent/tree/main/.claude/skills/collecting-flows-safely
Command: npx skills add https://github.com/citytexi/team-yg-pesonal-agent --skill collecting-flows-safely-citytexi

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Jetpack Compose screens that use collectAsState() keep collecting flows while the app is backgrounded, wasting CPU and battery, and composables that accept Flow<T> parameters block skipping and recompose on every emission. This Skill migrates those patterns to lifecycle-aware, conflated collection. ## Core Features & Use Cases - Lifecycle-aware migration: Replaces every collectAsState() with collectAsStateWithLifecycle() from androidx.lifecycle:lifecycle-runtime-compose, with guidance on minActiveState (STARTED vs RESUMED). - High-frequency flow tuning: Applies .conflate() and .distinctUntilChanged() inside remember for sensor, location, and websocket streams emitting faster than ~100 ms. - Antipattern removal: Hoists Flow<T> parameters out of composables so callers collect once and pass resolved values, and uses snapshotFlow for State-to-Flow bridges like analytics on visible list items. - Use Case: A user reports battery drain when a chat screen sits in the recents tray; the Skill audits all collectAsState calls, migrates them, and verifies via logcat that upstream work stops when backgrounded. ## Quick Start Ask the assistant to audit this Compose module for collectAsState usage and migrate every flow collection to collectAsStateWithLifecycle with proper conflation.

Frequently Asked Questions about collecting-flows-safely

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I replace collectAsState with collectAsStateWithLifecycle in Compose?▼

Add the androidx.lifecycle:lifecycle-runtime-compose dependency and swap each collectAsState() call for collectAsStateWithLifecycle(). For StateFlow no initial value is needed; cold flows and SharedFlow require an initialValue parameter.

collectAsState vs collectAsStateWithLifecycle: what is the difference?▼

collectAsState collects whenever the composable is in composition, including while backgrounded. collectAsStateWithLifecycle ties collection to a Lifecycle.State (default STARTED), pausing upstream work when the app is backgrounded to save CPU and battery.

Why is passing a Flow as a composable parameter bad?▼

Flow is an unstable type, so a Flow parameter blocks skipping for the entire composable and its collection lifecycle is unclear across recompositions. Collect at the caller with collectAsStateWithLifecycle and pass the resolved value, or a () -> T lambda for hot producers.

How do I handle high-frequency sensor flows in Jetpack Compose?▼

Apply .conflate() and .distinctUntilChanged() upstream of collectAsStateWithLifecycle, wrapped in remember(key) so the operator chain is not recreated per recomposition. Ensure the emitted type has a correct equals() for distinctUntilChanged to work.

When should I use snapshotFlow instead of derivedStateOf?▼

Use snapshotFlow inside a LaunchedEffect when the only consumer is a fire-and-forget side effect like analytics or logging. It reads state in a coroutine rather than the composition phase, avoiding a derived state slot and spurious recompositions.

When should I not use collectAsStateWithLifecycle?▼

Avoid it for flows created and consumed inside a single composable's remember block, for data already held as State<T>, and for truly always-on background listeners. Always-on work belongs in a Service, WorkManager, or repeatOnLifecycle in the Activity.