2

My app is not able to work in offline mode after I disconnected the internet. When I debugged, it failed at line 49(plz visit the screenshot attached) as shown highlighted. Somehow it is getting the realm instance at line 48 even in offline mode, but throwing error 'Object reference not set to an instance of an object' at the next line. How will I achieve the offline functionality from here? My code is in .Net.

Second scenario: I first connected to internet and then opened my app. It opened, then I turned internet off. Then I tested my application features and it was able to work without internet at that time. Even I came back online, it posted all the data that I created in offline mode.

        public async static void SubscribeProducts()
    {
        try
        {
            App.realmProducts = Realm.GetInstance(App.config);
            App.realmProducts.Subscriptions.Update(() =>
            {
                var products = App.realmProducts.All<Products>()/*.Where(t => !string.IsNullOrEmpty(t.AgentName))*/;
                App.realmProducts.Subscriptions.Add(products);
            });

            await App.realmProducts.Subscriptions.WaitForSynchronizationAsync();
        }
        catch (Exception ex)
        {

        }
    }

screenshot of code snippet and error

Next observation: while creating the instance and loggin in, it's failing at the login step, since it's in offline mode. But since I kept the block in try catch, it skipped through that block and I am not able to process any offline work. Is it because the login didn't work? Then I can say, atleast one time, my app has to appear online before I can do something. This is not intended I think.

        public async static void InitRealms()
    {
        try
        {
            var myRealmAppId = "application-2-dfsre";
            var app = Realms.Sync.App.Create(myRealmAppId);
            var user = await app.LogInAsync(Credentials.EmailPassword("arup@gmail.com", "aruparup"));
            config = new FlexibleSyncConfiguration(user);
        }
        catch (Exception ex)
        {

        }
    }
    public async static void SubscribeEntities()
    {
        realmEntities = Realm.GetInstance(config);
        realmEntities.Subscriptions.Update(() =>
        {
            entities = realmEntities.All<Entities0>()/*.Where(t => !string.IsNullOrEmpty(t.AgentName))*/;
            realmEntities.Subscriptions.Add(entities);
        });
        await realmEntities.Subscriptions.WaitForSynchronizationAsync();
    }
Arup
  • 155
  • 11

1 Answers1

2

After much investigation and thanks to this article of awesome mongodb https://www.mongodb.com/docs/realm/sdk/dotnet/examples/open-a-realm/ that says you have to use app.CurrentUser == null condition first before trying to log in. It will check if an already logged in user (obviously in online mode) is available now in cache. So by using this logic, it will find the user even in offline mode and will not go further to process the login once again.

    if (app.CurrentUser == null)
{
    // App must be online for user to authenticate
    user = await app.LogInAsync(
        Credentials.EmailPassword("caleb@mongodb.com", "shhhItsASektrit!"));
    config = new PartitionSyncConfiguration("_part", user);
    realm = await Realm.GetInstanceAsync(config);
}
else
{
    // This works whether online or offline
    user = app.CurrentUser;
    config = new PartitionSyncConfiguration("_part", user);
    realm = Realm.GetInstance(config);
}
Arup
  • 155
  • 11