diff --git a/osu.Game.Tests/Database/RealmSubscriptionRegistrationTests.cs b/osu.Game.Tests/Database/RealmSubscriptionRegistrationTests.cs index 45842a952ab3..e5be4d665b65 100644 --- a/osu.Game.Tests/Database/RealmSubscriptionRegistrationTests.cs +++ b/osu.Game.Tests/Database/RealmSubscriptionRegistrationTests.cs @@ -71,6 +71,35 @@ void onChanged(IRealmCollection sender, ChangeSet? changes) } } + [Test] + public void TestSubscriptionInitialChangeSetNull() + { + ChangeSet? firstChanges = null; + int receivedChangesCount = 0; + + RunTestWithRealm((realm, _) => + { + var registration = realm.RegisterForNotifications(r => r.All(), onChanged); + + realm.WriteAsync(r => r.Add(TestResources.CreateTestBeatmapSetInfo())).WaitSafely(); + + realm.Run(r => r.Refresh()); + + Assert.That(receivedChangesCount, Is.EqualTo(1)); + Assert.That(firstChanges, Is.Null); + + registration.Dispose(); + }); + + void onChanged(IRealmCollection sender, ChangeSet? changes) + { + if (receivedChangesCount == 0) + firstChanges = changes; + + receivedChangesCount++; + } + } + [Test] public void TestSubscriptionWithAsyncWrite() { diff --git a/osu.Game/Database/RealmObjectExtensions.cs b/osu.Game/Database/RealmObjectExtensions.cs index 72529ed9ff7c..2fa3b8a88072 100644 --- a/osu.Game/Database/RealmObjectExtensions.cs +++ b/osu.Game/Database/RealmObjectExtensions.cs @@ -65,7 +65,8 @@ public static class RealmObjectExtensions if (!d.Beatmaps.Contains(existingBeatmap)) { Debug.Fail("Beatmaps should never become detached under normal circumstances. If this ever triggers, it should be investigated further."); - Logger.Log("WARNING: One of the difficulties in a beatmap was detached from its set. Please save a copy of logs and report this to devs.", LoggingTarget.Database, LogLevel.Important); + Logger.Log("WARNING: One of the difficulties in a beatmap was detached from its set. Please save a copy of logs and report this to devs.", LoggingTarget.Database, + LogLevel.Important); d.Beatmaps.Add(existingBeatmap); } @@ -291,7 +292,24 @@ public static IDisposable QueryAsyncWithNotifications(this IRealmCollection + { + if (initial) + { + initial = false; + + // Realm might coalesce the initial callback, meaning we never receive a `ChangeSet` of `null` marking the first callback. + // Let's decouple it for simplicity in handling. + if (changes != null) + { + callback(sender, null); + return; + } + } + + callback(sender, changes); + })); } ///