开发者

How can I find out if a core-data object can be deleted when relationships could prevent it?

Let's say I have an employee database, where an employee can be manager 开发者_如何学Pythonof a department. The manager relationship of a department (to an employee in the role of a manager) is not optional. So, I cannot delete an employee when he is manager of a deparment.

In my UITableViewDelegate I would like to have:

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView
          editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self mayDeleteTableView:tableView entryAtIndexPath:indexPath] ?
            UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleNone;
}

Where mayDeleteTableView:entryAtIndexPath returns NO when the employee is manager of a department, otherwise YES.

Now, of course I can implement this method by setting up a fetch request that tells me what I want. But I just wondered if there is an easier way than that, because core-data must have some mechanism to find out exactly this, otherwise it could not raise an error when I delete an object that I shouldn't.

So my question is: "Does core data offer the implementation of mayDeleteTableView:entryAtIndexPath for me somehow?"

Update: Ok, actually I found the NSManagedObject:validateForDelete method now. It seems to return always NO. So my question now is: "Do I have to implement validateForDelete or should this do the correct verification for me (in which case I am doing something else wrong)?"


If you set up the manager attribute as a BOOL or something similar, you can check your tableView against that. After getting a reference to the employee through the tableView, something simple such as

return [[[fetchController objectAtIndexPath:indexPath] manager] boolValue] ? UITableViewCellEditingStyleNone : UITableViewCellEditingStyleDelete;

Here I'm assuming you get the core data object via NSFetchResultsController, but no matter how you gather it, this should be the same logic. I hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜