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

Conversation

peppy
Copy link
Sponsor Member

@peppy peppy commented Jul 9, 2024

We expect this to be the case, but it turns out that it may be coalesced:

Notifications are delivered via the standard event loop, and so can't
be delivered while the event loop is blocked by other activity. When
notifications can't be delivered instantly, multiple notifications may
be coalesced into a single notification. This can include the
notification with the initial collection.

Rather than struggle with handling this locally every time, let's fix the callback at our end to ensure we receive the initial null case.

I've raised concern for the API being a bit silly with realm (realm/realm-dotnet#3641).

peppy added 2 commits July 9, 2024 15:47
…angeSet`

We expect this to be the case, but it turns out that it [may be
coalesced](https://www.mongodb.com/docs/realm-sdks/dotnet/latest/reference/Realms.IRealmCollection-1.html#Realms_IRealmCollection_1_SubscribeForNotifications_Realms_NotificationCallbackDelegate__0__Realms_KeyPathsCollection_):

> Notifications are delivered via the standard event loop, and so can't
> be delivered while the event loop is blocked by other activity. When
> notifications can't be delivered instantly, multiple notifications may
> be coalesced into a single notification. This can include the
> notification with the initial collection.

Rather than struggle with handling this locally every time, let's fix
the callback at our end to ensure we receive the initial null case.

I've raised concern for the API being a bit silly with realm
(realm/realm-dotnet#3641).
@peppy peppy added the realm deals with local realm database label Jul 9, 2024
@peppy peppy added the blocked Don't merge this. label Jul 9, 2024
@peppy
Copy link
Sponsor Member Author

peppy commented Jul 9, 2024

Hold up this isn't correct.

Comment on lines 304 to 308
if (changes != null)
callback(sender, null);
}

callback(sender, changes);
Copy link
Contributor

@smoogipoo smoogipoo Jul 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If sender contains the items after the modifications are applied, then shouldn't we not send the non-null callback if initial == true?

To further expound, when you send (sender, null), the callsite will use those items to populate the initial list:

if (changes == null)
{
realmBeatmapSets.Clear();
realmBeatmapSets.AddRange(sender.Select(r => r.ID));

But then we send the (sender, changes) callback which could be used to e.g. delete from that "initial list" from above:

foreach (int i in changes.DeletedIndices.OrderDescending())
{
setsRequiringRemoval.Add(realmBeatmapSets[i]);
realmBeatmapSets.RemoveAt(i);
}

But the deleted indices are actually irrelevant here because we did not previously have an initial list.

It depends on what sender truly is in the case when the changes have been coalesced - is it the initial list before any modifications, or is it the list after all coalesced modifications.

Copy link
Sponsor Member Author

@peppy peppy Jul 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah just noticed this very thing in further testing. Fixed in ee9e329.

Copy link
Contributor

@smoogipoo smoogipoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me

@smoogipoo smoogipoo enabled auto-merge July 9, 2024 07:25
@smoogipoo smoogipoo disabled auto-merge July 9, 2024 07:52
@smoogipoo smoogipoo merged commit e734cdf into ppy:master Jul 9, 2024
11 of 17 checks passed
@peppy peppy deleted the fix-realm-callbacks branch July 10, 2024 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked Don't merge this. realm deals with local realm database size/M
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants