objective-c – 如何通过核心数据关系删除所有对象?

objective-c – 如何通过核心数据关系删除所有对象?,第1张

概述说我有一个自定义的NSManagedObject部门,这有一个属性表示与雇员即NSSet *雇员的一对多关系; 对于给定的部门,我想删除员工中的所有对象。请问建议/最好的方法是什么? 所以假设我的代码如下所示: Department.h @interface Department: NSManagedObject {}@property (retain) NSString *departmen 说我有一个自定义的NSManagedobject部门,这有一个属性表示与雇员即NSSet *雇员的一对多关系;

对于给定的部门,我想删除员工中的所有对象。请问建议/最好的方法是什么?

所以假设我的代码如下所示:

Department.h

@interface Department: NSManagedobject {}@property (retain) Nsstring *departmentname;@property (retain) NSSet *employees;@end

Department.m

@implementation Department@dynamic departmentname;@dynamic employees;

Employee.h

@interface Employee: NSManagedobject {}@property (retain) Nsstring *firstname;@property (retain) Nsstring *lastname;@property (retain) Department *worksIn;@end

doCoreDataStuff

- (voID)doCoreDataStuff:sender {    //add a department,give it a couple employees,then try to remove those employees    NSEntityDescription *deptEntity = [NSEntityDescription entityForname:@"Department"                                                 inManagedobjectContext:self.managedobjectContext];    Department *dept = [Department alloc] initWithEntity:deptEntity                          insertIntoManagedobjectContext:self.managedobjectContext];    NSError *error;    dept.departmentname = @"Accounting";    //save more often than normal to see more easily what causes error    if (![self.managedobjectContext save:&error]) NSLog(@"\nError: %@",[error localizedDescription]);    NSEntityDescription *empEntity = [NSEntityDescription entityForname:@"Employee"                                                 inManagedobjectContext:self.managedobjectContext];    emp.firstname = @"Steve";    emp.lastname = @"Smith";    emp.worksIn = dept;    if (![self.managedobjectContext save:&error]) NSLog(@"\nError: %@",[error localizedDescription]);    emp = [[Employee alloc] initWithEntity:empEntity            insertIntoManagedobjectContext:self.managedobjectContext];    emp.firstname = @"Natasha";    emp.lastname = @"Johnson";    emp.worksIn = dept;    if (![self.managedobjectContext save:&error]) NSLog(@"\nError: %@",[error localizedDescription]);    //all good so far! Now will try to delete all employees for this department    dept.employees = [NSSet set];    if (![self.managedobjectContext save:&error]) NSLog(@"\nError: %@",[error localizedDescription]); //"Multiple valIDation errors occurred."    //this also produces the same error    [[dept mutableSetValueForKey:@"employees"] removeAllObjects];    if (![self.managedobjectContext save:&error]) NSLog(@"\nError: %@",[error localizedDescription]); //"Multiple valIDation errors occurred."

员工之间的关系不是可选的,所以我猜想从部门中删除员工意味着我试图“孤立”员工,即将员工保留在持续的模式中,而不需要相关部门。

所以,我认为我的原始问题应该被改写为:当孩子与父母有非任意关系时,最好/推荐的方式来删除“父母”的所有“孩子”对象?

我怀疑答案是“一次循环并删除一个员工对象”。

UPDATE

根据答案和苹果文档的链接,我应该可以将删除规则设置为“Cascade”,然后代码如department.employees = [NSSet set];将工作。但是,在我已经设置相应的删除规则的非常简单的项目中,这不起作用。

谢谢

解决方法 如果要删除特定部门的员工元素,则可以像for for一样运行for-in循环

for (Employees * theEmployee in department.employees) {  [self.managedobjectContext deleteObject:[self.managedobjectContext objectWithID:theEmployee.objectID]]; }

然后保存您的托管上下文。如果当然这就是你想要的,而不是消除员工和部门之间的关系;在这种情况下,分配空集将起作用。

上述变化:

for (Employee *employeetoDelete in department.employees) {    [self.managedobjectContext deleteObject:employeetoDelete];}
总结

以上是内存溢出为你收集整理的objective-c – 如何通过核心数据关系删除所有对象?全部内容,希望文章能够帮你解决objective-c – 如何通过核心数据关系删除所有对象?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1038070.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存