首页 > 解决方案 > Xamarin Realm 退出时没有错误但没有结果

问题描述

我不知道如何调试这种行为。

我试图从我的领域数据库中检索一行,但是当我查询领域时,代码只是从方法中退出。

    public User GetUser(int id)
    {
        User user = null;

        try
        {
            if (RealmInstance.All<User>().Where(a => a.Id == id).Any())
            {
                user = RealmInstance.All<User>().Where(a => a.Id == id).First();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        return user;
    }

当我调试这个方法时,我看到调试器在这一行退出if(RealmInstance.All<User>().Where(a => a.Id == id).Any())并且没有抛出错误,它只是出去而不继续执行。

这不是我第一次面对这种行为,我不知道如何强制 Realm 让我获取一些日志以查看附加的内容。

编辑1:

如果将我的代码拆分为此

var users = RealmInstance.All<User>();

var userFound = users.Where(a => a.Id == id);

//var userExist = userFound.Any(); //This cause the code to exit from method without exception
var userExist = userFound.Count() > 0; //This is OK sometimes, and other no. When it's work, it do not exit from method.

if (userExist)
{
   user = users.Where(a => a.Id == id).First();
}

但我仍在寻找一种从 Realm 获得异常的方法

标签: xamarinrealm

解决方案


推荐阅读