Urgent.News

What's breaking now, across thousands of outlets.

Tech

collectAsState is quietly leaking your work

Here is a line you have written a hundred times: val state by viewModel . data . collectAsState () It looks harmless. It is the standard way to turn a Flow into Compose state. And on a screen with live data, it quietly keeps working long after the user has walked away. The screen nobody is watching collectAsState starts collecting the flow and stops only when the composable leaves the…

The collectAsState function in Compose is commonly used to turn a Flow into Compose state. However, this seemingly harmless line of code can quietly leak work in the background, even when the user is not interacting with the screen. When the app is backgrounded, the composable remains composed, causing the collection to keep running and the ViewModel to continue fetching, computing, and pushing new state.

This can lead to unexpected battery and mobile data usage, as well as bugs caused by stale changes being displayed when the user returns.

To resolve this issue, the collectAsStateWithLifecycle function should be used instead. This function ties the collection to the lifecycle owner, stopping the collection when the lifecycle drops below STARTED and resuming it when the lifecycle comes back. By making collectAsStateWithLifecycle the default and plain collectAsState the exception, developers can ensure that work stops when the screen is not visible, saving resources and preventing bugs.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Report Generation: Asynchronous Jobs, Validation, and Load Latency Explained

Short answer: use an explicit PDF job, validate the input before enqueueing it, and keep the output in a separate, short-lived location until the job is complete.

  • Create explicit PDF job queue boundary for handling generation, retries, and load latency.
  • Validate MIME type and file size before job creation, reject non-contract documents.
  • Implement observable queue with retries, idempotency keys, exponential backoff, and status polling.

The most useful exchange I had this week was with someone who has 7 followers

If you saw this account cold, you would scroll past it. FarzamHabibi. Bio: Product Designer. 7 followers. 9 public repos. Account created 2022-08-27.

  • FarzamHabibi, a product designer with 7 followers, shared a GitHub repository on 2022-08-27.
  • The author engaged in constructive feedback exchange, adding six items to the checklist.

More from Friday 4 September →