mirror of
https://github.com/ppy/osu.git
synced 2024-12-15 01:02:55 +08:00
Add automatic transaction handling to realm helper methods
This commit is contained in:
parent
d4c539687e
commit
5197d0fa9e
@ -59,6 +59,25 @@ namespace osu.Game.Tests.Database
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[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]
|
[Test]
|
||||||
public void TestAccessAfterAttach()
|
public void TestAccessAfterAttach()
|
||||||
{
|
{
|
||||||
|
@ -8,18 +8,45 @@ namespace osu.Game.Database
|
|||||||
{
|
{
|
||||||
public static class RealmExtensions
|
public static class RealmExtensions
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Perform a write operation against the provided realm instance.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// This will automatically start a transaction if not already in one.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="realm">The realm to operate on.</param>
|
||||||
|
/// <param name="function">The write operation to run.</param>
|
||||||
public static void Write(this Realm realm, Action<Realm> function)
|
public static void Write(this Realm realm, Action<Realm> function)
|
||||||
{
|
{
|
||||||
using var transaction = realm.BeginWrite();
|
Transaction? transaction = null;
|
||||||
|
|
||||||
|
if (!realm.IsInTransaction)
|
||||||
|
transaction = realm.BeginWrite();
|
||||||
|
|
||||||
function(realm);
|
function(realm);
|
||||||
transaction.Commit();
|
|
||||||
|
transaction?.Commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Perform a write operation against the provided realm instance.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// This will automatically start a transaction if not already in one.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="realm">The realm to operate on.</param>
|
||||||
|
/// <param name="function">The write operation to run.</param>
|
||||||
public static T Write<T>(this Realm realm, Func<Realm, T> function)
|
public static T Write<T>(this Realm realm, Func<Realm, T> function)
|
||||||
{
|
{
|
||||||
using var transaction = realm.BeginWrite();
|
Transaction? transaction = null;
|
||||||
|
|
||||||
|
if (!realm.IsInTransaction)
|
||||||
|
transaction = realm.BeginWrite();
|
||||||
|
|
||||||
var result = function(realm);
|
var result = function(realm);
|
||||||
transaction.Commit();
|
|
||||||
|
transaction?.Commit();
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user