核心数据 - DENY规则不会删除与删除的源对象相关的目标对象 [英] Core data - DENY rule is not deleting destination objects related to the source object deleted

查看:150
本文介绍了核心数据 - DENY规则不会删除与删除的源对象相关的目标对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表。任务和问题。两个表之间有太多的关系。

I have two tables. Assignments and Questions. Both the tables has too-many relationships between them. Delete rule from Assignments to Questions is DENY and from Questions to Assignments is Nullify.

删除指定时,我想删除与该作业相关的所有问题这些问题仍然有其他作业的参考。这是DENY删除规则从分配设置为问题的原因。

When an assignment is deleted, I want to delete all the questions related to that assignment leaving over the questions which still has a reference with other Assignments. This is the reason DENY delete rule is set from Assignment to Questions.

在删除分配之前,我确保我从分配中删除问题引用,如下所示: / p>

Before deleting Assignment I make sure that I remove the questions reference from assignment as shown in the below:

Assignment *assignment = assignment to be deleted;

for (SMQuestion *question in [assignment.questions allObjects])
{
//NSLog(@"The Question ID is %@",question.questionId);

//[question removeAssignmentsObject:assignment];
[assignment removeQuestionsObject:question];
//question.assignments = nil;
}

[[[CoreDataManager sharedObject] managedObjectContext] deleteObject:assignment];

[[[CoreDataManager sharedObject] managedObjectContext] save:&error];

但是删除分配后,没有一个问题会从数据库中删除。有什么我在这里做错了吗?

But after deleting assignment, none of the questions are getting deleted from the database. Is there anything which I am doing wrong here?

推荐答案

DENY规则不会导致任何对象被删除。

The DENY rule does not cause any object to be deleted. It only prevents the source object from being deleted if it has a relationship to the destination object.

在这种情况下(从分配到问题的DENY规则),分配不能是如果与任何问题相关,则删除。

In this case (DENY rule from Assignment to Question), an assignment cannot be deleted if it is related to any question.

您的代码只会删除作业中与问题相关的关系。

Your code removes only the relationships from the assignment to the questions. You have to check if the question has any other assigments left, and delete the question otherwise.

请注意 question.assignments = nil;

Note also that question.assignments = nil; in your loop removes all assignments from the question, not only the one "assignment to be deleted".

这篇关于核心数据 - DENY规则不会删除与删除的源对象相关的目标对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆