Skip to content

Fix clear completed tasks being undone by immediate refresh#1067

Open
saadkhalidkhan wants to merge 2 commits into
android:mainfrom
saadkhalidkhan:fix/clear-completed-refresh-race
Open

Fix clear completed tasks being undone by immediate refresh#1067
saadkhalidkhan wants to merge 2 commits into
android:mainfrom
saadkhalidkhan:fix/clear-completed-refresh-race

Conversation

@saadkhalidkhan
Copy link
Copy Markdown

@saadkhalidkhan saadkhalidkhan commented May 23, 2026

Summary

  • Remove unnecessary refresh() after clearCompletedTasks() in TasksViewModel.
  • The task list already updates via Room Flow.
  • An immediate full refresh can reload stale network data before saveTasksToNetwork() finishes (the fake network has a 2s delay), which can restore completed tasks the user just cleared.

Test plan

  • :app:testDebugUnitTest (TasksViewModelTest)
  • Manual: complete tasks, Clear completed from menu, verify completed tasks stay cleared

@google-cla
Copy link
Copy Markdown

google-cla Bot commented May 23, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request removes an unnecessary manual refresh call in the TasksViewModel after clearing completed tasks and updates the associated test to use advanceUntilIdle(). Feedback suggests improving the test's robustness by waiting for a specific state condition instead of using uiState.first(), which can be prone to race conditions when testing StateFlow.

@saadkhalidkhan
Copy link
Copy Markdown
Author

I signed it!

The tasks list already updates via Room Flow after clearing completed tasks. Calling refresh() immediately reloads stale network data before saveTasksToNetwork() finishes, which can restore deleted tasks.
Use uiState.first { !it.isLoading } instead of first() to avoid inspecting stale StateFlow values.
@saadkhalidkhan saadkhalidkhan force-pushed the fix/clear-completed-refresh-race branch from d977426 to a76293f Compare May 23, 2026 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant