【问题标题】:update and delete in realm xamarin在领域 xamarin 中更新和删除
【发布时间】:2017-06-28 08:13:33
【问题描述】:

我有一个以下 json,我在领域数据库中添加和更新

JSON

"listActivityTypeMaster": [
    {
        "id": "NV_22_06",
        "name": "NV_22_06"
    },
    {
        "id": "POC",
        "name": "POC"
    }
]

存储在json之上的类

public class ActivityType : RealmObject, IEntity
{
    [PrimaryKey]
    public string id { get; set; }
    public string name { get; set; }


}

创建和更新我使用以下领域声明

realm.WriteAsync(tempRealm =>{


    tempRealm.Add(data, true);


});

现在我想要的是,如果我的 JSON 通过删除一个条目而被更改,那么我的领域也应该在我触发 tempRealm.Add(data, true); 时删除该条目

"listActivityTypeMaster": [

    {
        "id": "POC",
        "name": "POC"
    }
]

我明白tempRealm.Add(data, true); 是用于更新记录,它会添加一个新的主键条目不存在,否则它将替换它。 但是如果主键不存在,有什么办法可以从领域中删除该记录

【问题讨论】:

    标签: c# xamarin realm


    【解决方案1】:

    这是不可能的,而且在极少数情况下是可取的。作为一种变通方法,更新后,您可以执行:

    var activities = realm.All<ActivityType>().ToArray();
    var ids = new HashSet<string>(data.Select(d => d.id));
    realm.Write(() =>
    {
        foreach (var activity in activities)
        {
            if (!ids.Contains(activity.Id))
            {
                realm.Remove(activity);
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多