1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 22:07:25 +08:00

Fix various cases of creating realm contexts from update thread when not necessary

This commit is contained in:
Dean Herbert 2022-01-21 01:34:20 +09:00
parent 0c9eb3ad61
commit a5d2047f05
7 changed files with 36 additions and 37 deletions

View File

@ -119,15 +119,17 @@ namespace osu.Game.Beatmaps
/// <param name="beatmapInfo">The beatmap difficulty to hide.</param> /// <param name="beatmapInfo">The beatmap difficulty to hide.</param>
public void Hide(BeatmapInfo beatmapInfo) public void Hide(BeatmapInfo beatmapInfo)
{ {
using (var realm = contextFactory.CreateContext()) contextFactory.Run(realm =>
using (var transaction = realm.BeginWrite())
{ {
if (!beatmapInfo.IsManaged) using (var transaction = realm.BeginWrite())
beatmapInfo = realm.Find<BeatmapInfo>(beatmapInfo.ID); {
if (!beatmapInfo.IsManaged)
beatmapInfo = realm.Find<BeatmapInfo>(beatmapInfo.ID);
beatmapInfo.Hidden = true; beatmapInfo.Hidden = true;
transaction.Commit(); transaction.Commit();
} }
});
} }
/// <summary> /// <summary>
@ -136,15 +138,17 @@ namespace osu.Game.Beatmaps
/// <param name="beatmapInfo">The beatmap difficulty to restore.</param> /// <param name="beatmapInfo">The beatmap difficulty to restore.</param>
public void Restore(BeatmapInfo beatmapInfo) public void Restore(BeatmapInfo beatmapInfo)
{ {
using (var realm = contextFactory.CreateContext()) contextFactory.Run(realm =>
using (var transaction = realm.BeginWrite())
{ {
if (!beatmapInfo.IsManaged) using (var transaction = realm.BeginWrite())
beatmapInfo = realm.Find<BeatmapInfo>(beatmapInfo.ID); {
if (!beatmapInfo.IsManaged)
beatmapInfo = realm.Find<BeatmapInfo>(beatmapInfo.ID);
beatmapInfo.Hidden = false; beatmapInfo.Hidden = false;
transaction.Commit(); transaction.Commit();
} }
});
} }
public void RestoreAll() public void RestoreAll()
@ -176,8 +180,7 @@ namespace osu.Game.Beatmaps
/// <returns>The first result for the provided query, or null if no results were found.</returns> /// <returns>The first result for the provided query, or null if no results were found.</returns>
public ILive<BeatmapSetInfo>? QueryBeatmapSet(Expression<Func<BeatmapSetInfo, bool>> query) public ILive<BeatmapSetInfo>? QueryBeatmapSet(Expression<Func<BeatmapSetInfo, bool>> query)
{ {
using (var context = contextFactory.CreateContext()) return contextFactory.Run(realm => realm.All<BeatmapSetInfo>().FirstOrDefault(query)?.ToLive(contextFactory));
return context.All<BeatmapSetInfo>().FirstOrDefault(query)?.ToLive(contextFactory);
} }
#region Delegation to BeatmapModelManager (methods which previously existed locally). #region Delegation to BeatmapModelManager (methods which previously existed locally).
@ -305,13 +308,13 @@ namespace osu.Game.Beatmaps
// If we seem to be missing files, now is a good time to re-fetch. // If we seem to be missing files, now is a good time to re-fetch.
if (importedBeatmap?.BeatmapSet?.Files.Count == 0) if (importedBeatmap?.BeatmapSet?.Files.Count == 0)
{ {
using (var realm = contextFactory.CreateContext()) contextFactory.Run(realm =>
{ {
var refetch = realm.Find<BeatmapInfo>(importedBeatmap.ID)?.Detach(); var refetch = realm.Find<BeatmapInfo>(importedBeatmap.ID)?.Detach();
if (refetch != null) if (refetch != null)
importedBeatmap = refetch; importedBeatmap = refetch;
} });
} }
return workingBeatmapCache.GetWorkingBeatmap(importedBeatmap); return workingBeatmapCache.GetWorkingBeatmap(importedBeatmap);

View File

@ -98,17 +98,16 @@ namespace osu.Game.Beatmaps
/// <returns>The first result for the provided query, or null if no results were found.</returns> /// <returns>The first result for the provided query, or null if no results were found.</returns>
public BeatmapInfo? QueryBeatmap(Expression<Func<BeatmapInfo, bool>> query) public BeatmapInfo? QueryBeatmap(Expression<Func<BeatmapInfo, bool>> query)
{ {
using (var context = ContextFactory.CreateContext()) return ContextFactory.Run(realm => realm.All<BeatmapInfo>().FirstOrDefault(query)?.Detach());
return context.All<BeatmapInfo>().FirstOrDefault(query)?.Detach();
} }
public void Update(BeatmapSetInfo item) public void Update(BeatmapSetInfo item)
{ {
using (var realm = ContextFactory.CreateContext()) ContextFactory.Run(realm =>
{ {
var existing = realm.Find<BeatmapSetInfo>(item.ID); var existing = realm.Find<BeatmapSetInfo>(item.ID);
realm.Write(r => item.CopyChangesToRealm(existing)); realm.Write(r => item.CopyChangesToRealm(existing));
} });
} }
} }
} }

View File

@ -386,11 +386,11 @@ namespace osu.Game.Overlays.Settings.Sections.Input
private void updateStoreFromButton(KeyButton button) private void updateStoreFromButton(KeyButton button)
{ {
using (var realm = realmFactory.CreateContext()) realmFactory.Run(realm =>
{ {
var binding = realm.Find<RealmKeyBinding>(((IHasGuidPrimaryKey)button.KeyBinding).ID); var binding = realm.Find<RealmKeyBinding>(((IHasGuidPrimaryKey)button.KeyBinding).ID);
realm.Write(() => binding.KeyCombinationString = button.KeyBinding.KeyCombinationString); realm.Write(() => binding.KeyCombinationString = button.KeyBinding.KeyCombinationString);
} });
} }
private void updateIsDefaultValue() private void updateIsDefaultValue()

View File

@ -74,8 +74,7 @@ namespace osu.Game.Scoring
public override bool IsAvailableLocally(ScoreInfo model) public override bool IsAvailableLocally(ScoreInfo model)
{ {
using (var context = ContextFactory.CreateContext()) return ContextFactory.Run(realm => realm.All<ScoreInfo>().Any(s => s.OnlineID == model.OnlineID));
return context.All<ScoreInfo>().Any(b => b.OnlineID == model.OnlineID);
} }
} }
} }

View File

@ -113,10 +113,10 @@ namespace osu.Game.Skinning
public void SelectRandomSkin() public void SelectRandomSkin()
{ {
using (var context = contextFactory.CreateContext()) contextFactory.Run(realm =>
{ {
// choose from only user skins, removing the current selection to ensure a new one is chosen. // choose from only user skins, removing the current selection to ensure a new one is chosen.
var randomChoices = context.All<SkinInfo>().Where(s => !s.DeletePending && s.ID != CurrentSkinInfo.Value.ID).ToArray(); var randomChoices = realm.All<SkinInfo>().Where(s => !s.DeletePending && s.ID != CurrentSkinInfo.Value.ID).ToArray();
if (randomChoices.Length == 0) if (randomChoices.Length == 0)
{ {
@ -127,7 +127,7 @@ namespace osu.Game.Skinning
var chosen = randomChoices.ElementAt(RNG.Next(0, randomChoices.Length)); var chosen = randomChoices.ElementAt(RNG.Next(0, randomChoices.Length));
CurrentSkinInfo.Value = chosen.ToLive(contextFactory); CurrentSkinInfo.Value = chosen.ToLive(contextFactory);
} });
} }
/// <summary> /// <summary>
@ -182,8 +182,7 @@ namespace osu.Game.Skinning
/// <returns>The first result for the provided query, or null if no results were found.</returns> /// <returns>The first result for the provided query, or null if no results were found.</returns>
public ILive<SkinInfo> Query(Expression<Func<SkinInfo, bool>> query) public ILive<SkinInfo> Query(Expression<Func<SkinInfo, bool>> query)
{ {
using (var context = contextFactory.CreateContext()) return contextFactory.Run(realm => realm.All<SkinInfo>().FirstOrDefault(query)?.ToLive(contextFactory));
return context.All<SkinInfo>().FirstOrDefault(query)?.ToLive(contextFactory);
} }
public event Action SourceChanged; public event Action SourceChanged;

View File

@ -165,8 +165,7 @@ namespace osu.Game.Stores
public override bool IsAvailableLocally(BeatmapSetInfo model) public override bool IsAvailableLocally(BeatmapSetInfo model)
{ {
using (var context = ContextFactory.CreateContext()) return ContextFactory.Run(realm => realm.All<BeatmapInfo>().Any(b => b.OnlineID == model.OnlineID));
return context.All<BeatmapInfo>().Any(b => b.OnlineID == model.OnlineID);
} }
public override string HumanisedModelName => "beatmap"; public override string HumanisedModelName => "beatmap";

View File

@ -165,7 +165,7 @@ namespace osu.Game.Stores
public bool Delete(TModel item) public bool Delete(TModel item)
{ {
using (var realm = ContextFactory.CreateContext()) return ContextFactory.Run(realm =>
{ {
if (!item.IsManaged) if (!item.IsManaged)
item = realm.Find<TModel>(item.ID); item = realm.Find<TModel>(item.ID);
@ -175,12 +175,12 @@ namespace osu.Game.Stores
realm.Write(r => item.DeletePending = true); realm.Write(r => item.DeletePending = true);
return true; return true;
} });
} }
public void Undelete(TModel item) public void Undelete(TModel item)
{ {
using (var realm = ContextFactory.CreateContext()) ContextFactory.Run(realm =>
{ {
if (!item.IsManaged) if (!item.IsManaged)
item = realm.Find<TModel>(item.ID); item = realm.Find<TModel>(item.ID);
@ -189,7 +189,7 @@ namespace osu.Game.Stores
return; return;
realm.Write(r => item.DeletePending = false); realm.Write(r => item.DeletePending = false);
} });
} }
public abstract bool IsAvailableLocally(TModel model); public abstract bool IsAvailableLocally(TModel model);