1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 19:27:24 +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>
public void Hide(BeatmapInfo beatmapInfo)
{
using (var realm = contextFactory.CreateContext())
using (var transaction = realm.BeginWrite())
contextFactory.Run(realm =>
{
if (!beatmapInfo.IsManaged)
beatmapInfo = realm.Find<BeatmapInfo>(beatmapInfo.ID);
using (var transaction = realm.BeginWrite())
{
if (!beatmapInfo.IsManaged)
beatmapInfo = realm.Find<BeatmapInfo>(beatmapInfo.ID);
beatmapInfo.Hidden = true;
transaction.Commit();
}
beatmapInfo.Hidden = true;
transaction.Commit();
}
});
}
/// <summary>
@ -136,15 +138,17 @@ namespace osu.Game.Beatmaps
/// <param name="beatmapInfo">The beatmap difficulty to restore.</param>
public void Restore(BeatmapInfo beatmapInfo)
{
using (var realm = contextFactory.CreateContext())
using (var transaction = realm.BeginWrite())
contextFactory.Run(realm =>
{
if (!beatmapInfo.IsManaged)
beatmapInfo = realm.Find<BeatmapInfo>(beatmapInfo.ID);
using (var transaction = realm.BeginWrite())
{
if (!beatmapInfo.IsManaged)
beatmapInfo = realm.Find<BeatmapInfo>(beatmapInfo.ID);
beatmapInfo.Hidden = false;
transaction.Commit();
}
beatmapInfo.Hidden = false;
transaction.Commit();
}
});
}
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>
public ILive<BeatmapSetInfo>? QueryBeatmapSet(Expression<Func<BeatmapSetInfo, bool>> query)
{
using (var context = contextFactory.CreateContext())
return context.All<BeatmapSetInfo>().FirstOrDefault(query)?.ToLive(contextFactory);
return contextFactory.Run(realm => realm.All<BeatmapSetInfo>().FirstOrDefault(query)?.ToLive(contextFactory));
}
#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 (importedBeatmap?.BeatmapSet?.Files.Count == 0)
{
using (var realm = contextFactory.CreateContext())
contextFactory.Run(realm =>
{
var refetch = realm.Find<BeatmapInfo>(importedBeatmap.ID)?.Detach();
if (refetch != null)
importedBeatmap = refetch;
}
});
}
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>
public BeatmapInfo? QueryBeatmap(Expression<Func<BeatmapInfo, bool>> query)
{
using (var context = ContextFactory.CreateContext())
return context.All<BeatmapInfo>().FirstOrDefault(query)?.Detach();
return ContextFactory.Run(realm => realm.All<BeatmapInfo>().FirstOrDefault(query)?.Detach());
}
public void Update(BeatmapSetInfo item)
{
using (var realm = ContextFactory.CreateContext())
ContextFactory.Run(realm =>
{
var existing = realm.Find<BeatmapSetInfo>(item.ID);
realm.Write(r => item.CopyChangesToRealm(existing));
}
});
}
}
}

View File

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

View File

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

View File

@ -113,10 +113,10 @@ namespace osu.Game.Skinning
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.
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)
{
@ -127,7 +127,7 @@ namespace osu.Game.Skinning
var chosen = randomChoices.ElementAt(RNG.Next(0, randomChoices.Length));
CurrentSkinInfo.Value = chosen.ToLive(contextFactory);
}
});
}
/// <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>
public ILive<SkinInfo> Query(Expression<Func<SkinInfo, bool>> query)
{
using (var context = contextFactory.CreateContext())
return context.All<SkinInfo>().FirstOrDefault(query)?.ToLive(contextFactory);
return contextFactory.Run(realm => realm.All<SkinInfo>().FirstOrDefault(query)?.ToLive(contextFactory));
}
public event Action SourceChanged;

View File

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

View File

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