Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure realm subscriptions always fire initial callback with null ChangeSet #28785

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions osu.Game.Tests/Database/RealmSubscriptionRegistrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,35 @@ void onChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet? changes)
}
}

[Test]
public void TestSubscriptionInitialChangeSetNull()
{
ChangeSet? firstChanges = null;
int receivedChangesCount = 0;

RunTestWithRealm((realm, _) =>
{
var registration = realm.RegisterForNotifications(r => r.All<BeatmapSetInfo>(), 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<BeatmapSetInfo> sender, ChangeSet? changes)
{
if (receivedChangesCount == 0)
firstChanges = changes;

receivedChangesCount++;
}
}

[Test]
public void TestSubscriptionWithAsyncWrite()
{
Expand Down
22 changes: 20 additions & 2 deletions osu.Game/Database/RealmObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -291,7 +292,24 @@ public static IDisposable QueryAsyncWithNotifications<T>(this IRealmCollection<T
if (!RealmAccess.CurrentThreadSubscriptionsAllowed)
throw new InvalidOperationException($"Make sure to call {nameof(RealmAccess)}.{nameof(RealmAccess.RegisterForNotifications)}");

return collection.SubscribeForNotifications(callback);
bool initial = true;
return collection.SubscribeForNotifications(((sender, changes) =>
{
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);
}));
}

/// <summary>
Expand Down
Loading