diff --git a/osu.Game.Tests/Database/RealmLiveTests.cs b/osu.Game.Tests/Database/RealmLiveTests.cs
index a50eb22c67..d15e038723 100644
--- a/osu.Game.Tests/Database/RealmLiveTests.cs
+++ b/osu.Game.Tests/Database/RealmLiveTests.cs
@@ -59,6 +59,25 @@ public void TestAccessAfterStorageMigrate()
});
}
+ [Test]
+ public void TestNestedWriteCalls()
+ {
+ RunTestWithRealm((realm, _) =>
+ {
+ var beatmap = new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata());
+
+ var liveBeatmap = beatmap.ToLive(realm);
+
+ realm.Run(r =>
+ r.Write(_ =>
+ r.Write(_ =>
+ r.Add(beatmap)))
+ );
+
+ Assert.IsFalse(liveBeatmap.PerformRead(l => l.Hidden));
+ });
+ }
+
[Test]
public void TestAccessAfterAttach()
{
diff --git a/osu.Game/Database/RealmExtensions.cs b/osu.Game/Database/RealmExtensions.cs
index 73e9f16d33..2cd81b6af1 100644
--- a/osu.Game/Database/RealmExtensions.cs
+++ b/osu.Game/Database/RealmExtensions.cs
@@ -8,18 +8,45 @@ namespace osu.Game.Database
{
public static class RealmExtensions
{
+ ///
+ /// Perform a write operation against the provided realm instance.
+ ///
+ ///
+ /// This will automatically start a transaction if not already in one.
+ ///
+ /// The realm to operate on.
+ /// The write operation to run.
public static void Write(this Realm realm, Action function)
{
- using var transaction = realm.BeginWrite();
+ Transaction? transaction = null;
+
+ if (!realm.IsInTransaction)
+ transaction = realm.BeginWrite();
+
function(realm);
- transaction.Commit();
+
+ transaction?.Commit();
}
+ ///
+ /// Perform a write operation against the provided realm instance.
+ ///
+ ///
+ /// This will automatically start a transaction if not already in one.
+ ///
+ /// The realm to operate on.
+ /// The write operation to run.
public static T Write(this Realm realm, Func function)
{
- using var transaction = realm.BeginWrite();
+ Transaction? transaction = null;
+
+ if (!realm.IsInTransaction)
+ transaction = realm.BeginWrite();
+
var result = function(realm);
- transaction.Commit();
+
+ transaction?.Commit();
+
return result;
}