我有一个名为Task的实体,它与实体TaskTemplate和Occurrence具有一对一的关系。这两个实体都与Tasks有一对多的关系。删除规则设置为无效。
在Occurrence中,我尝试删除所有任务。为此,我使用以下代码:
- (bool)removeAllTasks
{
NSMutableSet *tasks = [self mutableSetValueForKey:@"tasks"];
[tasks removeAllObjects];
return YES;
}
这不起作用。以下是尝试删除之前的任务和发生:
Relationship 'tasks' on managed object (0x1006bd1c0) <Occurrence: 0x1006bd1c0> (entity: Occurrence; id: 0x102119510 <x-coredata://6A0ECEF9-473D-44B2-9E89-AC15179D7761/Occurrence/p1> ; data: {
... bunch of properties ...
taskType = "0x10211a020 <x-coredata://6A0ECEF9-473D-44B2-9E89-AC15179D7761/TaskTemplate/p2>";
tasks = (
"0x1021185e0 <x-coredata://6A0ECEF9-473D-44B2-9E89-AC15179D7761/Task/p3>"
);
}) with objects {(
<Task: 0x10c31d700> (entity: Task; id: 0x1021185e0 <x-coredata://6A0ECEF9-473D-44B2-9E89-AC15179D7761/Task/p3> ; data: {
... some more properties ...
occurrence = "0x102119510 <x-coredata://6A0ECEF9-473D-44B2-9E89-AC15179D7761/Occurrence/p1>";
persons = "<relationship fault: 0x10e049e90 'persons'>";
startDate = "2013-01-28 11:00:00 +0000";
type = "0x10211a020 <x-coredata://6A0ECEF9-473D-44B2-9E89-AC15179D7761/TaskTemplate/p2>";
})
)}
...这是之后的结果-removeAllObjects
:
Relationship 'tasks' on managed object (0x1006bd1c0) <Occurrence: 0x1006bd1c0> (entity: Occurrence; id: 0x102119510 <x-coredata://6A0ECEF9-473D-44B2-9E89-AC15179D7761/Occurrence/p1> ; data: {
... bunch of properties ...
taskType = "0x10211a020 <x-coredata://6A0ECEF9-473D-44B2-9E89-AC15179D7761/TaskTemplate/p2>";
tasks = (
);
}) with objects {(
)}
请注意,任务关系现在是空的,正如我所期望的那样。但我也希望taskType为空。我究竟做错了什么?
编辑:
我更改了代码,以便查看该任务是否标记为已删除。我现在有了:
- (bool)removeAllEvents
{
NSMutableSet *tasks = [self mutableSetValueForKey:@"tasks"];
[tasks enumerateObjectsUsingBlock:^(Task *task, BOOL *stop)
{
if (![task isDeleted])
[self.managedObjectContext deleteObject:task];
NSLog(@"Deleted %@", [task isDeleted] ? @"Y" : @"N");
}];
[self.managedObjectContext processPendingChanges];
return YES;
}
它确实显示“Y”,表示该任务已标记为删除。fetchRequest 显示上下文中没有任务。但是如果我尝试保存,我会收到一条不一致的消息(“任务无效”)。