Coredata迁移:既有属性又有属性&独特的
我有一个称为nameentity
的实体,它具有可选的属性unique IdentiDifier
。现在,我想使此属性不可行,也是独特的,以避免重复的条目。
我做了什么,
- 我创建了一个模型版本
- 的
unique IdentiDifier
属性(使其非访问) - 添加到
约束
name> nameentity
Entity的部分 e节- (将其设置为唯一属性) - 创建了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
- I created a Model version
- Unchecked optional for
uniqueIdentifier
attribute (To make it non-optional) - Added uniqueIdentifier to
constraints
section ofNameEntity
entity - (To set it as unique attribute) - 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论