Fix chart showing No Data on rapid period switching#1816
Conversation
Ignore cancelled task errors instead of setting error state
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the robustness of chart and portfolio data loading by implementing cooperative task cancellation. The changes ensure that when a user rapidly changes the selected period, previous, now-irrelevant data fetching tasks are properly cancelled and their potential errors are ignored, preventing the UI from showing incorrect 'No Data' or error messages and providing a smoother user experience. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request addresses an issue where charts show 'No Data' during rapid period switching by ignoring errors from cancelled tasks. The approach of adding cancellation points with Task.checkCancellation() and handling cancellation in catch blocks is correct. However, the implementation can be made more robust. Instead of checking Task.isCancelled, it's safer to specifically check if the caught error is a CancellationError. This prevents accidentally swallowing other legitimate errors that might occur while the task is in a cancelled state. I've added suggestions to make this change in all affected files.
| ) | ||
| chartState = .data(model) | ||
| } catch { | ||
| guard !Task.isCancelled else { return } |
There was a problem hiding this comment.
While checking Task.isCancelled works for the intended purpose of ignoring cancellation errors, it can lead to swallowing non-cancellation errors if the task happens to be cancelled when another error is thrown. A more robust approach is to check if the caught error is specifically a CancellationError.
| guard !Task.isCancelled else { return } | |
| guard !(error is CancellationError) else { return } |
| } | ||
| state = .data(data) | ||
| } catch { | ||
| guard !Task.isCancelled else { return } |
There was a problem hiding this comment.
While checking Task.isCancelled works for the intended purpose of ignoring cancellation errors, it can lead to swallowing non-cancellation errors if the task happens to be cancelled when another error is thrown. A more robust approach is to check if the caught error is specifically a CancellationError.
| guard !Task.isCancelled else { return } | |
| guard !(error is CancellationError) else { return } |
| let chart = ChartValuesViewModel.priceChange(charts: charts, period: selectedPeriod, formatter: currencyFormatter, showHeaderValue: true) | ||
| state = chart.map { .data(WalletPortfolioData(chart: $0, portfolio: portfolio)) } ?? .noData | ||
| } catch { | ||
| guard !Task.isCancelled else { return } |
There was a problem hiding this comment.
While checking Task.isCancelled works for the intended purpose of ignoring cancellation errors, it can lead to swallowing non-cancellation errors if the task happens to be cancelled when another error is thrown. A more robust approach is to check if the caught error is specifically a CancellationError.
| guard !Task.isCancelled else { return } | |
| guard !(error is CancellationError) else { return } |
| assetId: assetModel.asset.id, | ||
| period: selectedPeriod | ||
| ) | ||
| try Task.checkCancellation() |
There was a problem hiding this comment.
try Task.checkCancellation() is unnecessary in here - task(id:) already propagates cancellation through try await
| state = .loading | ||
| do { | ||
| let data = try await perpetualService.portfolio(address: address) | ||
| try Task.checkCancellation() |
| do { | ||
| let rate = try priceService.getRate(currency: currencyCode) | ||
| let portfolio = try await service.getPortfolioAssets(assets: assets, period: selectedPeriod) | ||
| try Task.checkCancellation() |
| ) | ||
| chartState = .data(model) | ||
| } catch { | ||
| guard !Task.isCancelled else { return } |
There was a problem hiding this comment.
lets use for consistency?
if !error.isCancelled {
chartState = .error(error)
}
Extract cancellation-aware error handling into a reusable setError method on StateViewType, replacing manual `if !error.isCancelled` checks across 10 call sites.
Ignore cancelled task errors instead of setting error state