Use request cancellation and IsDisposed guard instead of more Schedule

This commit is contained in:
Dean Herbert
2022-09-26 16:02:33 +09:00
parent 0fa5e1b28e
commit 32d56fe3a9
2 changed files with 19 additions and 11 deletions

View File

@ -185,10 +185,15 @@ namespace osu.Game.Online.Leaderboards
if (scores != null) if (scores != null)
this.scores.AddRange(scores); this.scores.AddRange(scores);
// Non-delayed schedule may potentially run inline (due to IsMainThread check passing) after leaderboard is disposed.
// This is guarded against in BeatmapLeaderboard via web request cancellation, but let's be extra safe.
if (!IsDisposed)
{
// Schedule needs to be non-delayed here for the weird logic in refetchScores to work. // Schedule needs to be non-delayed here for the weird logic in refetchScores to work.
// If it is removed, the placeholder will be incorrectly updated to "no scores" rather than "retrieving". // If it is removed, the placeholder will be incorrectly updated to "no scores" rather than "retrieving".
// This whole flow should be refactored in the future. // This whole flow should be refactored in the future.
Scheduler.Add(applyNewScores, false); Scheduler.Add(applyNewScores, false);
}
void applyNewScores() void applyNewScores()
{ {

View File

@ -89,6 +89,8 @@ namespace osu.Game.Screens.Select.Leaderboards
private IDisposable scoreSubscription; private IDisposable scoreSubscription;
private GetScoresRequest scoreRetrievalRequest;
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load() private void load()
{ {
@ -151,15 +153,14 @@ namespace osu.Game.Screens.Select.Leaderboards
else if (filterMods) else if (filterMods)
requestMods = mods.Value; requestMods = mods.Value;
var req = new GetScoresRequest(fetchBeatmapInfo, fetchRuleset, Scope, requestMods); scoreRetrievalRequest = new GetScoresRequest(fetchBeatmapInfo, fetchRuleset, Scope, requestMods);
// Schedule is required to avoid potential object disposed exception when LoadComponentAsync is eventually called. scoreRetrievalRequest.Success += response => SetScores(
req.Success += r => Schedule(() => SetScores( scoreManager.OrderByTotalScore(response.Scores.Select(s => s.ToScoreInfo(rulesets, fetchBeatmapInfo))),
scoreManager.OrderByTotalScore(r.Scores.Select(s => s.ToScoreInfo(rulesets, fetchBeatmapInfo))), response.UserScore?.CreateScoreInfo(rulesets, fetchBeatmapInfo)
r.UserScore?.CreateScoreInfo(rulesets, fetchBeatmapInfo) );
));
return req; return scoreRetrievalRequest;
} }
protected override LeaderboardScore CreateDrawableScore(ScoreInfo model, int index) => new LeaderboardScore(model, index, IsOnlineScope) protected override LeaderboardScore CreateDrawableScore(ScoreInfo model, int index) => new LeaderboardScore(model, index, IsOnlineScope)
@ -219,7 +220,9 @@ namespace osu.Game.Screens.Select.Leaderboards
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)
{ {
base.Dispose(isDisposing); base.Dispose(isDisposing);
scoreSubscription?.Dispose(); scoreSubscription?.Dispose();
scoreRetrievalRequest?.Cancel();
} }
} }
} }