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;
|
|
|
|
|
2022-03-08 05:42:59 +00:00
|
|
|
#nullable enable
|
|
|
|
|
2021-01-11 09:58:56 +00:00
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
|
|
|
public static class RealmExtensions
|
|
|
|
{
|
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
|
|
|
{
|
2021-09-30 14:42:40 +00:00
|
|
|
using var transaction = realm.BeginWrite();
|
|
|
|
function(realm);
|
|
|
|
transaction.Commit();
|
2021-01-11 10:46:51 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2021-09-30 14:42:40 +00:00
|
|
|
using var transaction = realm.BeginWrite();
|
|
|
|
var result = function(realm);
|
|
|
|
transaction.Commit();
|
|
|
|
return result;
|
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
|
|
|
}
|
|
|
|
}
|