简体   繁体   English

核心数据不允许我删除没有关系对象的对象

[英]Core Data won't let me delete an object with no relationship objects

I have a fairly straightforward data model for a Core Data app. 对于核心数据应用程序,我有一个非常简单的数据模型。 Basically, I'm bringing up a list of contacts already on the phone. 基本上,我要列出电话中已有的联系人列表。 When a user taps a contact, they're taken to a detail view screen, where they can select a phone number or email address. 当用户点击联系人时,他们将被带到详细信息查看屏幕,他们可以在其中选择电话号码或电子邮件地址。 When they select any detail item, I'm storing it via Core Data. 当他们选择任何详细信息时,我将通过Core Data进行存储。

I have one entity called Contact , and another called Contact_Detail . 我有一个实体叫做Contact ,另一个实体叫Contact_Detail

Contact <----->> Contact_Detail 联系方式<----- >> Contact_Detail

The Contact relationship to Contact_Detail has a delete rule of Deny. 与Contact_Detail的联系人关系具有删除规则“拒绝”。 The Contact_Detail relationship to Contact has a delete rule of No Action. 与Contact的Contact_Detail关系的删除规则为“不执行”。

If a user wants to remove, say, a previously selected detail item, I first fetch the Contact_Detail objects that have the same ID as the Contact. 如果用户想删除某个先前选择的详细信息,则首先获取与该ID具有相同ID的Contact_Detail对象。 If there is only one Contact_Detail object returned, I then know I need to remove the contact as well from the Contact entity. 如果仅返回一个Contact_Detail对象,那么我知道我也需要从Contact实体中删除该联系人。 If there were more than one Contact_Detail object returned, I just remove that particular object. 如果返回了多个Contact_Detail对象, Contact_Detail删除该特定对象。

It's all working fine, and the database is getting updated as expected (detail objects removed, and when the last detail object for a particular contact is removed, so is the contact) as long as I just go back and forth between the peoplePicker and the peopleDetailViewController. 一切正常,并且数据库正在按预期进行更新(删除了详细对象,并且删除了特定联系人的最后一个详细对象时,联系人也是如此),只要我在peoplePicker和peopleDetailViewController。

The problem I'm having is that when I select an email address, leave the detailVC, leave the peoplePicker, then return back to the peoplePicker, and back to the same contact, and deselect the email address I previously selected, Core Data is throwing an error that looks as though it can't delete my Contact because there are still relationship objects (the Contact_Detail ): 我遇到的问题是,当我选择一个电子邮件地址时,离开detailVC,离开peoplePicker,然后返回到peoplePicker,再回到同一联系人,然后取消选择我之前选择的电子邮件地址,Core Data就会抛出该错误。似乎无法删除我的Contact的错误,因为仍然存在关系对象( Contact_Detail ):

NSValidationErrorKey=details, NSLocalizedDescription=The operation couldn’t be completed. (Cocoa error 1600.), NSValidationErrorValue=Relationship objects for {(
     (entity: Contact_Detail; id: 0xd21afe0  ; data: )
)} on 0x5d5a780}

I can verify that the Contact_Detail object does in fact get removed. 我可以验证Contact_Detail对象是否确实已被删除。 Why can't the Contact be removed? 为什么无法删除联系人? And why does this only happen with a particular sequence of navigation? 为什么只在特定的导航序列中发生这种情况?

Let me know if I should post some code. 让我知道是否应该发布一些代码。 I think this problem is causing a crash I'm seeing after continuing to work with Core Data after this first error is thrown. 我认为这个问题在引发第一个错误后继续使用Core Data之后会导致崩溃。

Thanks! 谢谢!

Why don't you set delete rules in this way? 为什么不以这种方式设置删除规则?

Contact<--(cascade)-->>ContactDetail
//when Contact is removed, all details are removed too)

ContactDetail<<--(nullify)-->Contact
//when Contact Detail is removed, this particular detail is removed from Contact too

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM