collecting-flows-safely

Migrate Compose UI flow collection to lifecycle-aware operators.

8|Updated May 18, 2025
One-click install
npx skills add https://github.com/decoutkhanqindev/DexReader --skill collecting-flows-safely-decoutkhanqindev
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: collecting-flows-safely
Source: https://github.com/decoutkhanqindev/DexReader/tree/main/.claude/skills/collecting-flows-safely
Command: npx skills add https://github.com/decoutkhanqindev/DexReader --skill collecting-flows-safely-decoutkhanqindev

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill helps developers improve the performance of their Jetpack Compose UIs by correctly collecting flows and avoiding common pitfalls.

Core Features & Use Cases

  • Flow Collection Optimization: Migrate from collectAsState() to collectAsStateWithLifecycle() for lifecycle-aware flow collection.
  • Flow Parameter Hoisting: Hoist Flow<T> parameters out of composables to prevent composition issues.
  • Flow Conflation & Distinct Until Changed: Apply .conflate() and .distinctUntilChanged() to reduce unnecessary recompositions.
  • Use Case: If you're experiencing background battery drain or UI invalidations due to chatty flows, this Skill can help you identify and fix the issue.

Quick Start

Analyze your Compose UI for unnecessary flow collections and apply lifecycle-aware flow collection to improve performance.

Frequently Asked Questions about collecting-flows-safely

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

FAQPage Schema
How do I optimize Jetpack Compose flow collection to reduce battery drain?

To optimize Jetpack Compose flow collection and reduce battery drain, migrate UI consumers from `collectAsState()` to `collectAsStateWithLifecycle()` and apply `.conflate()` or `.distinctUntilChanged()` to prevent unnecessary recompositions from chatty flows.

Why does my Compose UI experience unnecessary recompositions from StateFlow?

Your Compose UI experiences unnecessary recompositions from StateFlow because standard `collectAsState()` continues collecting in the background. Applying `.distinctUntilChanged()` and migrating to lifecycle-aware collection prevents these redundant UI invalidations.

What is the best way to collect SharedFlow in Jetpack Compose without causing background battery drain?

The best way to collect SharedFlow without causing background battery drain is using `collectAsStateWithLifecycle()`. This lifecycle-aware approach pauses flow collection when the UI is inactive, preventing wasted resources.

How do I fix the Flow as composable parameter antipattern in Kotlin?

To fix the Flow as composable parameter antipattern in Kotlin, hoist `Flow<T>` parameters out of composables. Collect the flow at the parent level and pass the resolved state down to prevent composition lifecycle issues.

When should I apply conflate to sensor and location streams in Compose?

You should apply `.conflate()` to sensor and location streams in Compose when stream emission rates exceed the UI's rendering speed. This drops intermediate values, keeping only the latest state to reduce UI invalidations.

Do I need knowledge of Kotlin coroutines to use collectAsStateWithLifecycle?

Yes, you need knowledge of Kotlin coroutines and Jetpack Compose to use `collectAsStateWithLifecycle()`. The migration process relies on understanding coroutine flow operators and lifecycle boundaries to safely optimize UI performance.