删除特定Realm Object Swift中的所有数据

时间:2015-05-28 18:29:22

标签: swift persistence realm

在我深入探讨我的问题之前。我的目标可能会影响您的答案,如果数据不再存在于云中,则会删除Object数据。

所以,如果我有一个数组["one", "two", "three"]

然后在我的服务器中删除"two"

我希望我的领域能够更新更改。

我认为最好的方法是删除特定Object中的所有数据,然后调用我的REST API下载新数据。如果有更好的方法,请告诉我。

好的,这是我的问题。

我有一个对象Notifications()

每次调用我的REST API时,在下载任何我正在运行的内容之前:

let realm = Realm()
let notifications = Notifications()
realm.beginWrite()
realm.delete(notifications)
realm.commitWrite()

运行后我收到此错误:Can only delete an object from the Realm it belongs to.

所以我试过这样的事情:

for notification in notifications {
    realm.delete(notification)
}
realm.commitWrite()

我在xcode中遇到的错误是:"Type Notifications does not conform to protocol 'SequenceType'

不确定从哪里开始。

试图找出领域。全新的

注意:realm.deleteAll()有效,但我不希望我的所有领域都被删除,只是确定Objects

1 个答案:

答案 0 :(得分:26)

您正在寻找:

iframe

或者也许这样:

src

虽然理想情况下,您将在CalendarIframe上设置主键,以便您可以简单地更新这些现有对象,而不是采用极端方法来对所有本地对象进行核对,只需重新创建它们(或几乎)