0

我有一个名为Task的实体,它与实体TaskTemplateOccurrence具有一对一的关系。这两个实体都与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 显示上下文中没有任务。但是如果我尝试保存,我会收到一条不一致的消息(“任务无效”)。

4

1 回答 1

2

您的removeAllTasks方法不会删除任何对象。它只删除了从Occurrence对象到Task对象的关系,但任务继续存在。删除规则仅在删除对象时应用(使用deleteObject:)。

于 2013-05-23T10:58:14.223 回答