Coredata迁移:既有属性又有属性&独特的

发布于 2025-01-31 17:24:33 字数 970 浏览 4 评论 0原文

我有一个称为nameentity的实体,它具有可选的属性unique IdentiDifier。现在,我想使此属性不可行,也是独特的,以避免重复的条目。

我做了什么,

  1. 我创建了一个模型版本
  2. unique IdentiDifier属性(使其非访问)
  3. 添加到约束 name> nameentity Entity的部分 e节- (将其设置为唯一属性)
  4. 创建了V1至V2的绘图模型,在其中添加了删除该实体中重复条目的迁移策略。

有了所有这些更改,一旦我运行该应用程序,应用程序就会通过显示以下错误来崩溃,

Cannot migrate store in-place: constraint violation during attempted migration
NSUnderlyingError: Error Domain=NSCocoaErrorDomain Code=134111 "(null)" 
UserInfo={_NSCoreDataOptimisticLockingFailureConflictsKey=(), 
NSUnderlyingException=Constraint unique violation: UNIQUE constraint failed: ZNAMEENTITY.ZUNIQUEIDENTIFIER, 
reason=constraint violation during attempted migration, 
NSExceptionOmitCallstacks=true}

但是,如果我设置/保留suoltyidentifier的可选复选框,则应用不会崩溃并通过迁移过程。这避免了重复的条目,但仍然允许零值。

因此,如何制作一个可选的属性,同时又有; Coredata迁移中的独特之处?

I have an entity called NameEntity, it has optional attribute uniqueIdentifier. Now I want to make this attribute non-optional as well as unique to avoid duplicate entries.

What I did

  1. I created a Model version
  2. Unchecked optional for uniqueIdentifier attribute (To make it non-optional)
  3. Added uniqueIdentifier to constraints section of NameEntity entity - (To set it as unique attribute)
  4. Created a MappingModel for v1 to v2 where I added Migration policy that deletes the duplicate entries in that entity.

With all these changes, as soon as I run the app, app crashes by showing the below error,

Cannot migrate store in-place: constraint violation during attempted migration
NSUnderlyingError: Error Domain=NSCocoaErrorDomain Code=134111 "(null)" 
UserInfo={_NSCoreDataOptimisticLockingFailureConflictsKey=(), 
NSUnderlyingException=Constraint unique violation: UNIQUE constraint failed: ZNAMEENTITY.ZUNIQUEIDENTIFIER, 
reason=constraint violation during attempted migration, 
NSExceptionOmitCallstacks=true}

But, If I set/keep the Optional checkbox of uniqueIdentifier, app doesn't crash and goes through the migration process. This avoids duplicate entries BUT still allows the nil values.

So, How can I make an optional attribute both non-optional & unique in the CoreData migration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文