I have an entity called Task which has a to-one relationship with the entities TaskTemplate and Occurrence. Both of these entities have to-many relationships with Tasks. Deletion rules are set to Nullify.
Within an Occurrence, I try to remove all Tasks. To do that, I use the following code:
- (bool)removeAllTasks
{
NSMutableSet *tasks = [self mutableSetValueForKey:@"tasks"];
[tasks removeAllObjects];
return YES;
}
This does not work. Here are Task and Occurrence before the attempted removal:
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>";
})
)}
... and this is the result after -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 {(
)}
Notice that the tasks relationship is now empty, as I would expect it to be. But I would also expect taskType to be empty. What am I doing wrong?
EDIT:
I changed the code so I can see if the task is marked as deleted. I have now:
- (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;
}
It does show 'Y', to indicate that the task is marked for deletion. A fetchRequest shows me that there are no tasks left in the context. But if I try to save, I get an inconsistency message ("tasks is not valid").