diff --git a/osu.Game/Database/RealmLive.cs b/osu.Game/Database/RealmLive.cs index 4f7bdf93e4..90b8814c24 100644 --- a/osu.Game/Database/RealmLive.cs +++ b/osu.Game/Database/RealmLive.cs @@ -24,20 +24,16 @@ namespace osu.Game.Database /// private readonly T data; - private readonly RealmContextFactory? realmFactory; + private readonly RealmContextFactory realmFactory; /// /// Construct a new instance of live realm data. /// /// The realm data. /// The realm factory the data was sourced from. May be null for an unmanaged object. - public RealmLive(T data, RealmContextFactory? realmFactory) + public RealmLive(T data, RealmContextFactory realmFactory) { this.data = data; - - if (IsManaged && realmFactory == null) - throw new ArgumentException(@"Realm factory must be provided for a managed instance", nameof(realmFactory)); - this.realmFactory = realmFactory; ID = data.ID; @@ -55,9 +51,6 @@ namespace osu.Game.Database return; } - if (realmFactory == null) - throw new ArgumentException(@"Realm factory must be provided for a managed instance", nameof(realmFactory)); - using (var realm = realmFactory.CreateContext()) perform(realm.Find(ID)); } @@ -74,9 +67,6 @@ namespace osu.Game.Database if (!IsManaged) return perform(data); - if (realmFactory == null) - throw new ArgumentException(@"Realm factory must be provided for a managed instance", nameof(realmFactory)); - using (var realm = realmFactory.CreateContext()) return perform(realm.Find(ID)); } @@ -108,7 +98,7 @@ namespace osu.Game.Database if (!ThreadSafety.IsUpdateThread) throw new InvalidOperationException($"Can't use {nameof(Value)} on managed objects from non-update threads"); - return realmFactory!.Context.Find(ID); + return realmFactory.Context.Find(ID); } } diff --git a/osu.Game/Database/RealmLiveUnmanaged.cs b/osu.Game/Database/RealmLiveUnmanaged.cs new file mode 100644 index 0000000000..5a69898206 --- /dev/null +++ b/osu.Game/Database/RealmLiveUnmanaged.cs @@ -0,0 +1,44 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using Realms; + +#nullable enable + +namespace osu.Game.Database +{ + /// + /// Provides a method of working with unmanaged realm objects. + /// Usually used for testing purposes where the instance is never required to be managed. + /// + /// The underlying object type. + public class RealmLiveUnmanaged : ILive where T : RealmObjectBase, IHasGuidPrimaryKey + { + /// + /// Construct a new instance of live realm data. + /// + /// The realm data. + public RealmLiveUnmanaged(T data) + { + Value = data; + } + + public bool Equals(ILive? other) => ID == other?.ID; + + public Guid ID => Value.ID; + + public void PerformRead(Action perform) => perform(Value); + + public TReturn PerformRead(Func perform) => perform(Value); + + public void PerformWrite(Action perform) => throw new InvalidOperationException(@"Can't perform writes on a non-managed underlying value"); + + public bool IsManaged => false; + + /// + /// The original live data used to create this instance. + /// + public T Value { get; } + } +} diff --git a/osu.Game/Database/RealmObjectExtensions.cs b/osu.Game/Database/RealmObjectExtensions.cs index c546a70fae..e5177823ba 100644 --- a/osu.Game/Database/RealmObjectExtensions.cs +++ b/osu.Game/Database/RealmObjectExtensions.cs @@ -56,13 +56,13 @@ namespace osu.Game.Database public static List> ToLiveUnmanaged(this IEnumerable realmList) where T : RealmObject, IHasGuidPrimaryKey { - return realmList.Select(l => new RealmLive(l, null)).Cast>().ToList(); + return realmList.Select(l => new RealmLiveUnmanaged(l)).Cast>().ToList(); } public static ILive ToLiveUnmanaged(this T realmObject) where T : RealmObject, IHasGuidPrimaryKey { - return new RealmLive(realmObject, null); + return new RealmLiveUnmanaged(realmObject); } public static List> ToLive(this IEnumerable realmList, RealmContextFactory realmContextFactory)