C#-如何在领域中删除行-Android Xamarin [英] C# - how to delete row in realm - android xamarin

查看:77
本文介绍了C#-如何在领域中删除行-Android Xamarin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了我创建的此方法,但提示我错误:

i tried this method that I created but it prompts me an error:

Realms.RealmInvalidObjectException:此对象是分离的.它是从领域中删除的吗?'

    public void deleteFromDatabase(List<CashDenomination> denom_list)
    {
        using (var transaction = Realm.GetInstance(config).BeginWrite())
        {
            Realm.GetInstance(config).Remove(denom_list[0]);
            transaction.Commit();
        }
    }

使用C#类型的编码从领域中删除数据库中记录的正确编码是什么?

what is the proper coding for deleting records from database in realm in C# type of coding?

推荐答案

您正在按照正确的方式进行操作.您收到的错误消息表明该对象已被删除.您确定它仍然存在于领域中吗?

You are doing it the right way. The error message you are getting indicates that the object was removed already. Are you sure it still exists in the realm?

更新:

我决定更新此答案,因为我对其他答案的评论有点难以理解.

您的原始代码应该可以正常工作.但是,如果您希望deleteFromDatabase接受具有CashDenomination实例的列表,这些实例或者已经被删除,或者可能从未添加到领域中,则需要添加一个检查.此外,请注意,您应该保留Realm实例,并在创建的事务中使用它.在大多数情况下,尽管通过GetInstance获取它的开销很小,但您希望将其保留更长的时间.

Your original code should work fine. However, if you want deleteFromDatabase to accept lists with CashDenomination instances that either have been removed already or perhaps were never added to the realm, you would need to add a check. Furthermore, note that you should hold on to your Realm instance and use it in the transaction you created. In most cases, you want to keep it around even longer, though there is little overhead to obtaining it via GetInstance.

public void deleteFromDatabase(List<CashDenomination> denom_list)
{
    if (!denom_list[0].IsValid) // If this object is not in the realm, do nothing.
        return;

    var realm = Realm.GetInstance(config);
    using (var transaction = realm.BeginWrite())
    {
        realm.Remove(denom_list[0]);
        transaction.Commit();
    }
}

现在,如果您想使用标识符,则可以像查找它一样进行查找,但仍只需使用Remove:

Now, if you want to use identifiers, you could look it up like you do, but still just use Remove:

public void deleteFromDatabase(int denom_id)
{
    var realm = Realm.GetInstance(config);

    var denom = realm.All<CashDenomination>().FirstOrDefault(c => c.denom_id == denom_id);
    if (denom == null)  // If no entry with this id exists, do nothing.
        return;

    using (var transaction = realm.BeginWrite())
    {
        realm.Remove(denom);
        transaction.Commit();
    }
}

最后,如果您的CashDenominationdenom_id标记为PrimaryKey,则可以这样查找:

Finally, if your CashDenomination has denom_id marked as PrimaryKey, you could look it up like this:

public void deleteFromDatabase(int denom_id)
{
    var realm = Realm.GetInstance(config);

    var denom = realm.ObjectForPrimaryKey<CashDenomination>(denom_id);
    if (denom == null)  // If no entry with this id exists, do nothing.
        return;

    using (var transaction = realm.BeginWrite())
    {
        realm.Remove(denom);
        transaction.Commit();
    }
}

这篇关于C#-如何在领域中删除行-Android Xamarin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆