Subscriptions.State resolves to Complete even when the network is down

I wanted to have two events - once when local database file is ready, and one is when it’s synced. I thought it would be easy just to raise the first before await realm.Subscriptions.WaitForSynchronizationAsync(); and one right after, but the thing is, both of them resolve even if I don’t have internet connection! Why would WaitForSynchronizationAsync resolve if it didn’t wait and why Subscriptions.State is Complete when without the connection there is no way to know whether it’s synced or no, is this by design?

Hi. It’d be helpful if you:

  1. tell us what language is that you’re using
  2. show us the code that you’re having issues with
  3. tell us what you’re exactly trying to achieve e.g. what do you mean by “local database is ready”? etc. etc.

Hello @Andrea_Catalini

It’s realm-dotnet:

Here’s the code:

     _app = App.Create(new AppConfiguration(myRealmAppId)
            {
                BaseFilePath = FileService.AppDataDirectory,
            });

            //_user = await _app.LogInAsync(Credentials.Anonymous());
            try
            {
                _user = await _app.LogInAsync(Credentials.EmailPassword("email", "pass"));
            }
            catch (Exception ex)
            {
                throw;
            }

            _config = new FlexibleSyncConfiguration(_user, Path.Combine(FileService.AppDataDirectory, FileService.DatabaseName))
            {
                PopulateInitialSubscriptions = (realm) =>
                {
                    ....
                    realm.Subscriptions.Add(realm.All<Entities.Word>());
                }
            };
            DatabaseInitialized?.Invoke();
            await GetRealm().Subscriptions.WaitForSynchronizationAsync();
            DatabaseSynced?.Invoke();

By “local database ready” I meant opening and getting ready the .realm file.
The code above by design should raise the initialized event when the file is opened and ready and the second event when the file is synchronized with server, but the WaitForSynchronizationAsync resolves even if there is no network, which by my logic it shouldn’t, it should throw an error as it specifically says in its name to wait for synchronization, otherwise it imho it should be renamed to something like TryWaitForSync…

Ok, now I can help you.

About WaitForSynchronizationAsync returning even without internet connection sounds like a bug.
If you’re sure that you’re effectively testing without an internet connection, could you open an issue on our github repo with attached a repro project?
Thank you.



As a side note, you don’t need WaitForSynchronizationAsync unless you’ve updated an active subscription.
When you use PopulateInitialSubscriptions you’re actually bootstrapping a realm with an initial subscription. This means that when you open a synced realm the initial subscription is going to be honored by downloading the matching elements.
So you’d do something like

_app = App.Create(new AppConfiguration(myRealmAppId)
{
    BaseFilePath = FileService.AppDataDirectory,
});

_user = await _app.LogInAsync(Credentials.EmailPassword("email", "pass"));

_config = new FlexibleSyncConfiguration(_user, Path.Combine(FileService.AppDataDirectory, FileService.DatabaseName))
{
    PopulateInitialSubscriptions = (realm) =>
    {
        ....
        realm.Subscriptions.Add(realm.All<Entities.Word>());
    }
};

// The process will complete when all the user's items have been downloaded.
var realm = await Realm.GetInstanceAsync(config);

When Realm.GetInstanceAsync returns, the realm is synchronized and ready to use.

If you were wondering what happens when you open a synced realm while offline I’ll just quote what’s in our docs as it’s concise and well written:

To open a synced realm, you must have an authenticated User object. To obtain an initial User instance, you need to authenticate against the Atlas App Services backend, which requires the device to be online the first time a user logs in. Once initial authentication has occurred, you can retrieve an existing user while offline.


Andrea

It looks like the problem is caused by WaitForSynchronizationAsync() and Subscriptions.The presence of a state in your code might be related to the library’s design. These functions may resolve early, even without an internet connection, which might be a restriction or design choice of the library. Further examination, as well as maybe contacting the library’s maintainers, may reveal insights into this behavior.

Thank you,
for sharing such good information. :innocent:

Hello, I really appreciate writing this type of content. This content is useful for me. keep sharing this type of content. Thanks :heart_eyes: