2021-01-11 09:58:56 +00:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
2021-09-30 14:42:40 +00:00
|
|
|
using System;
|
2021-01-11 09:58:56 +00:00
|
|
|
using Realms;
|
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
|
|
|
public static class RealmExtensions
|
|
|
|
{
|
2023-08-16 06:36:31 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Performs a <see cref="Realm.Find{T}(System.Nullable{long})"/>.
|
|
|
|
/// If a match was not found, a <see cref="Realm.Refresh"/> is performed before trying a second time.
|
|
|
|
/// This ensures that an instance is found even if the realm requested against was not in a consistent state.
|
|
|
|
/// </summary>
|
2023-08-16 07:40:46 +00:00
|
|
|
/// <param name="realm">The realm to operate on.</param>
|
|
|
|
/// <param name="id">The ID of the entity to find in the realm.</param>
|
|
|
|
/// <typeparam name="T">The type of the entity to find in the realm.</typeparam>
|
|
|
|
/// <returns>
|
|
|
|
/// The retrieved entity of type <typeparamref name="T"/>.
|
|
|
|
/// Can be <see langword="null"/> if the entity is still not found by <paramref name="id"/> even after a refresh.
|
|
|
|
/// </returns>
|
2023-08-16 06:36:31 +00:00
|
|
|
public static T? FindWithRefresh<T>(this Realm realm, Guid id) where T : IRealmObject
|
|
|
|
{
|
|
|
|
var found = realm.Find<T>(id);
|
|
|
|
|
|
|
|
if (found == null)
|
|
|
|
{
|
|
|
|
// It may be that we access this from the update thread before a refresh has taken place.
|
2023-08-16 07:40:46 +00:00
|
|
|
// To ensure that behaviour matches what we'd expect (the object generally *should be* available), force
|
2023-08-16 06:36:31 +00:00
|
|
|
// a refresh to bring in any off-thread changes immediately.
|
|
|
|
realm.Refresh();
|
|
|
|
found = realm.Find<T>(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2022-07-07 08:32:48 +00:00
|
|
|
/// <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>
|
2021-09-30 14:42:40 +00:00
|
|
|
public static void Write(this Realm realm, Action<Realm> function)
|
2021-01-11 09:58:56 +00:00
|
|
|
{
|
2022-07-07 08:32:48 +00:00
|
|
|
Transaction? transaction = null;
|
|
|
|
|
2022-07-07 09:15:15 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!realm.IsInTransaction)
|
|
|
|
transaction = realm.BeginWrite();
|
2022-07-07 08:32:48 +00:00
|
|
|
|
2022-07-07 09:15:15 +00:00
|
|
|
function(realm);
|
2022-07-07 08:32:48 +00:00
|
|
|
|
2022-07-07 09:15:15 +00:00
|
|
|
transaction?.Commit();
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
transaction?.Dispose();
|
|
|
|
}
|
2021-01-11 10:46:51 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 08:32:48 +00:00
|
|
|
/// <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>
|
2021-09-30 14:42:40 +00:00
|
|
|
public static T Write<T>(this Realm realm, Func<Realm, T> function)
|
2021-01-11 09:58:56 +00:00
|
|
|
{
|
2022-07-07 08:32:48 +00:00
|
|
|
Transaction? transaction = null;
|
|
|
|
|
2022-07-07 09:15:15 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!realm.IsInTransaction)
|
|
|
|
transaction = realm.BeginWrite();
|
2022-07-07 08:32:48 +00:00
|
|
|
|
2022-07-07 09:15:15 +00:00
|
|
|
var result = function(realm);
|
2022-07-07 08:32:48 +00:00
|
|
|
|
2022-07-07 09:15:15 +00:00
|
|
|
transaction?.Commit();
|
2022-07-07 08:32:48 +00:00
|
|
|
|
2022-07-07 09:15:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
transaction?.Dispose();
|
|
|
|
}
|
2021-01-11 09:58:56 +00:00
|
|
|
}
|
2022-03-08 05:42:59 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether the provided change set has changes to the top level collection.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Realm subscriptions fire on both collection and property changes (including *all* nested properties).
|
|
|
|
/// Quite often we only care about changes at a collection level. This can be used to guard and early-return when no such changes are in a callback.
|
|
|
|
/// </remarks>
|
|
|
|
public static bool HasCollectionChanges(this ChangeSet changes) => changes.InsertedIndices.Length > 0 || changes.DeletedIndices.Length > 0 || changes.Moves.Length > 0;
|
2021-01-11 09:58:56 +00:00
|
|
|
}
|
|
|
|
}
|