C# 在域xamarin中更新和删除
我在realm数据库中添加并更新了以下json JSON 类,该类存储在json之上C# 在域xamarin中更新和删除,c#,xamarin,realm,C#,Xamarin,Realm,我在realm数据库中添加并更新了以下json JSON 类,该类存储在json之上 public class ActivityType : RealmObject, IEntity { [PrimaryKey] public string id { get; set; } public string name { get; set; } } 为了创建和更新,我使用下面的realm语句 realm.WriteAsync(tempRealm =>{
public class ActivityType : RealmObject, IEntity
{
[PrimaryKey]
public string id { get; set; }
public string name { get; set; }
}
为了创建和更新,我使用下面的realm语句
realm.WriteAsync(tempRealm =>{
tempRealm.Add(data, true);
});
现在我想要的是,如果我的JSON通过删除一个条目而改变,那么我的领域也应该在触发tempRealm.Add(data,true)时删除该条目代码>
我确实理解tempRealm.Add(data,true)
用于更新记录,它将添加一个主键不存在的新条目,否则将替换它。
但如果主键不存在,是否有任何方法可以将该记录从域中删除,这是不可能的,并且很少有需要这样做的情况。作为解决方法,更新后,您可以执行:
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);
}
}
});
var activities=realm.All().ToArray();
varids=newhashset(data.Select(d=>d.id));
realm.Write(()=>
{
foreach(活动中的var活动)
{
如果(!ids.Contains(activity.Id))
{
领域。移除(活动);
}
}
});
"listActivityTypeMaster": [
{
"id": "POC",
"name": "POC"
}
]
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);
}
}
});