Skip to content

🚀: Add warning about using .launchIn() inside whileSubscribed block #204

@dimagor555

Description

@dimagor555

Description

When using .launchIn(this) inside a whileSubscribed block, the flow collection is not cancelled when subscribers drop to zero. This happens because launchIn() launches a coroutine in the
store's scope and returns immediately, so the whileSubscribed block completes without waiting for the flow.

The result is that the flow continues running indefinitely in the background, even when the screen is closed and there are no subscribers. This causes bugs like duplicate side effects,
stale state updates, and memory leaks.

The correct approach is to use .consume() which is a suspending function that properly respects cancellation.

Request

  1. Add a prominent warning in the documentation (red banner/callout) for the whileSubscribed plugin explicitly stating that .launchIn() should never be used inside the block, and .consume() should be used instead.
  2. Add a lint check to the FlowMVI IDE plugin that detects usage of .launchIn() inside whileSubscribed blocks and reports it as a warning or error, suggesting to use .consume() instead.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions