.net - 无法使用 .net sdk 在 Realm 中离线工作

标签 .net realm maui realm-mobile-platform real-mode

断开互联网连接后,我的应用无法在离线模式下运行。当我调试时,它在第 49 行失败(请访问所附的屏幕截图),如突出显示的那样。 不知何故,即使在离线模式下,它也在第 48 行获取 Realm 实例,但在下一行抛出错误“对象引用未设置为对象的实例”。我将如何从这里实现离线功能?我的代码位于 .Net 中。

第二种情况:我首先连接到互联网,然后打开我的应用程序。它打开了,然后我关掉了互联网。然后我测试了我的应用程序功能,当时它可以在没有互联网的情况下工作。即使我重新上线,它也会发布我在离线模式下创建的所有数据。

        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

下一步观察:创建实例并登录时,登录步骤失败,因为它处于离线模式。但由于我将该 block 保留在 try catch 中,因此它跳过了该 block ,并且我无法处理任何离线工作。是因为登录失败吗?然后我可以说,至少有一次,我的应用程序必须出现在网上,然后我才能做某事。我认为这不是有意的。

        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("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5534272025153238343c397b363a38" rel="noreferrer noopener nofollow">[email protected]</a>", "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();
    }

最佳答案

经过大量调查并感谢这篇很棒的 mongodb 文章 https://www.mongodb.com/docs/realm/sdk/dotnet/examples/open-a-realm/这表示您必须在尝试登录之前首先使用 app.CurrentUser == null 条件。它将检查缓存中是否存在已登录的用户(显然处于在线模式)。因此,通过使用这个逻辑,即使在离线模式下,它也会找到用户,并且不会进一步处理再次登录。

    if (app.CurrentUser == null)
{
    // App must be online for user to authenticate
    user = await app.LogInAsync(
        Credentials.EmailPassword("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="54373538313614393b3a333b30367a373b39" rel="noreferrer noopener nofollow">[email protected]</a>", "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);
}

关于.net - 无法使用 .net sdk 在 Realm 中离线工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74345630/

相关文章:

c# - FontSize.PIXELS c# 等价物

ios - Realm - 过滤大于字符串属性的对象

realm - 如何让keycloak导出 Realm 用户然后退出

mvvm - 如何根据.net maui 中的条件进行绑定(bind)?

c# - 为什么 Task.Factory.StartNew() 需要使用 CancellationToken 重载?

c# - 编译错误集合被修改

swift - 在 Swift 中将数组保存到 Realm ?

xamarin - MAUI Shell 从侧面移除 OverScroll

c# - .NET MAUI - Controller 句柄地址

c# - 如何仅在 .net Forms 2.0 CF 中绘制可视内容