【发布时间】:2017-07-08 04:53:36
【问题描述】:
我想将一个对象从一个领域复制到另一个领域,但它抱怨已经有一个具有该主键的对象。
详情:
我的应用中有两个不同的领域。一个存储在 Caches 文件夹中,另一个存储在 Documents 文件夹中。它们具有相同的架构。我想将一个对象从一个领域复制到另一个领域。按照文档中的指南,我使用Realm().create(_:value:update:) 来制作副本。当我从 ContentRealm 传递要在 UserRealm 中创建的故事时,我收到一个异常,指出具有主键的对象已经存在。但是,在 UserRealm 中尚不存在 Story 类型的实例,因此不应存在主键冲突。 Terminating app due to uncaught exception 'RLMException', reason: 'Attempting to create an object of type 'Story' with an existing primary key value '4'.'
我做错了吗?
// Copy the Story to the user database
var userStory: Story?
do {
try userRealm.write {
userStory = userRealm.create(Story.self, value:storyObjectFromContentRealm)
}
}
catch {
print("Could not copy story: \(error)")
}
【问题讨论】:
-
也许,userRealm 与您尝试从中复制的领域文件相同。您是否验证过 userRealm 的配置 url 与其他领域不同