Docs 菜单
Docs 主页
/ /
Atlas Device SDKs
/ /

删除数据 - .NET SDK

在此页面上

  • 删除对象
  • 删除多个对象
  • 删除对象及其依赖对象
  • 删除特定类型的所有对象
  • 删除 Realm 中的所有对象

例子

以下代码展示了如何从 Realm 中删除一个对象:

realm.Write(() =>
{
// Remove the instance from the realm.
realm.Remove(dog);
// Discard the reference.
dog = null;
});

例子

以下代码演示了如何从域中删除collection:

realm.Write(() =>
{
// Find dogs younger than 2 years old.
var puppies = realm.All<Dog>().Where(dog => dog.Age < 2);
// Remove the collection from the realm.
realm.RemoveRange(puppies);
});

有时,您希望在删除父对象时删除某些依赖对象。 我们将此称为链式删除。 Realm 不会为您删除依赖对象。 如果您不自行删除这些对象,它们在您的 Realm 中将保持孤立状态。 这是否是一个问题取决于应用程序的需求。

目前,删除依赖对象的最佳方法是遍历依赖项并在删除父对象之前删除依赖对象。

例子

以下代码演示了如何执行链式删除,首先删除 Ali 的所有狗,然后删除 Ali:

realm.Write(() =>
{
// Remove all of Ali's dogs.
realm.RemoveRange(ali.Dogs);
// Remove Ali.
realm.Remove(ali);
});

Realm 支持从 域 中删除Realm 类型的所有实例。

例子

以下代码演示了如何从 Realm 中删除所有 Dog 实例:

realm.Write(() =>
{
// Remove all instances of Dog from the realm.
realm.RemoveAll<Dog>();
});

可以从 Realm 中删除所有对象。 这不会影响 Realm 的模式。 这对于在原型设计时快速清理 Realm 非常有用。

例子

以下代码演示了如何删除 Realm 中的所有内容:

realm.Write(() =>
{
// Remove all objects from the realm.
realm.RemoveAll();
});
← 更新数据 - .NET SDK