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

Incorrect Subscriptions.State #3180

Closed
movsar opened this issue Jan 18, 2023 · 7 comments
Closed

Incorrect Subscriptions.State #3180

movsar opened this issue Jan 18, 2023 · 7 comments

Comments

@movsar
Copy link

movsar commented Jan 18, 2023

What happened?

realm.Subscriptions.State is set to Complete even when the network is down

Repro steps

After getting a realm instance, log realm.Subscriptions.State while network is down.

Version

.NET 6

What Atlas Services are you using?

Atlas Device Sync

What type of application is this?

Other

Client OS and version

Windows 10

Code snippets

No response

Stacktrace of the exception/crash you're getting

No response

Relevant log output

No response

@nirinchev
Copy link
Member

Can you share the exact code you're using? Generally speaking, the state would be complete if:

  1. You don't have any subscriptions - e.g. you open the Realm and immediately check the subscription state. Since there's no data that needs to be synchronized, the subscription state is "Complete".
  2. You have previously synchronized with the server and the server is doing "steady state synchronization" - i.e. it has sent the initial batch of data that matched your subscriptions and is only sending incremental changes now.

If your use case falls under any of these cases, then this is the expected behavior.

@nirinchev nirinchev self-assigned this Jan 18, 2023
@nirinchev nirinchev added the More-information-needed More information is needed to progress. The issue will close automatically in 2 weeks. label Jan 20, 2023
@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Jan 24, 2023
@movsar
Copy link
Author

movsar commented Jan 27, 2023

@nirinchev I open the database with the following config:

new FlexibleSyncConfiguration(appUser, FileService.DatabasePath)
            {
                SchemaVersion = 1,
                PopulateInitialSubscriptions = (realm) =>
                {
                    Debug.WriteLine($"APP: Realm : PopulateInitialSubscriptions");

                    realm.Subscriptions.Add(realm.All<Entities.Entry>());
                    realm.Subscriptions.Add(realm.All<Entities.Language>());
                    realm.Subscriptions.Add(realm.All<Entities.Phrase>());
                    realm.Subscriptions.Add(realm.All<Entities.Source>());
                    realm.Subscriptions.Add(realm.All<Entities.Translation>());
                    realm.Subscriptions.Add(realm.All<Entities.User>());
                    realm.Subscriptions.Add(realm.All<Entities.Word>());
                }
            };

as you can see, I have Subscriptions, and considering that the use case I reported is observed even on network loss, there is absolutely no way for realm to ascertain that the sync is complete, it has to connect to server to do that.

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed More-information-needed More information is needed to progress. The issue will close automatically in 2 weeks. Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Jan 27, 2023
@nirinchev
Copy link
Member

There are two things to consider here:

  1. PopulateInitialSubscriptions is only invoked once - i.e. when the Realm has been created. Is it possible that you created the Realm file before adding those subscriptions? Easiest way to check this would be to inspect realm.Subscriptions and check if their number matches your expectations.
  2. There are two phases of synchronization when using Flexible Sync. First is the bootstrap phase, which occurs after you update your subscriptions. The server will send back all documents that match the requested subscriptions at the time the subscription update was received. Once that is done, we move on to steady-state synchronization, which sends data back and forth as it comes. The Complete state pertains only the first phase - i.e. the bootstrap phase. Once it is complete, the subscription set is marked as Complete and it'll remain like this until you change the subscriptions. This is why even if you're offline, you can get Complete there.

@movsar
Copy link
Author

movsar commented Jan 27, 2023

The Complete state pertains only the first phase - i.e. the bootstrap phase. Once it is complete, the subscription set is marked as Complete and it'll remain like this until you change the subscriptions. This is why even if you're offline, you can get Complete there.

That's interesting, yes, this is exactly the case, but that's not what I would expect from that method, because my app subscribes to essentially all of the entities with PopulateInitialSubscription, and I expected that WaitForSynchronization() would wait when any of the items in these subscriptions change, not subscriptions themselves as I don't need to change them if they include all objects, right?

So, in my case, there is no way to wait for the changes, is there? Or should I use SyncSession's WaitForDownload() instead?

@nirinchev
Copy link
Member

Correct - if a subscription is in a Complete state but you want to ensure that you have the absolute latest state from the server, you need to call realm.SyncSession.WaitForDownload.

@movsar movsar closed this as completed Jan 27, 2023
@sync-by-unito sync-by-unito bot removed the Needs-Attention Reporter has responded. Review comment. label Jan 27, 2023
@movsar
Copy link
Author

movsar commented Jan 30, 2023

@nirinchev Is this also by design? That SyncSession is Active despite being in the Airplane mode?
image

@movsar movsar reopened this Jan 30, 2023
@nirinchev
Copy link
Member

nirinchev commented Jan 30, 2023

Yes, the session state indicates whether the session is attempting to communicate with the server. You are probably looking for ConnectionState which should indicate whether there's connection to the server.

@movsar movsar closed this as completed Jan 30, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants