How to automatically setup Core Data relationship when using nested contexts

前端 未结 2 1008
一向
一向 2021-02-08 08:32

I\'m struggling to figure out a decent solution to a problem that arises when using nested Managed Object Contexts in Core Data. Take a model that has two enites, Person and Nam

2条回答
  •  心在旅途
    2021-02-08 08:56

    The first thought that comes to mind is that while Name's person relationship is non-optional, you didn't say that Person's name relationship is also non-optional. Is it OK to create a Person with no Name, dispose with your code, and then create the Name later on when you actually need it?

    If not, one simple way is just to check whether you're on the root context before creating a Name:

    - (void)awakeFromInsert
    {
        [super awakeFromInsert];
    
        NSManagedObjectContext *context = [self managedObjectContext];
        if ([context parentContext] != nil) {
            self.name = [NSEntityDescription insertNewObjectForEntityForName:@"Name" inManagedObjectContext:context];
        }
    }
    

    But that only works if you always create new instances on a child context and you never nest contexts more than one level deep.

    What I'd probably do instead is create a method like the insertNewPersonInManagedObjectContext: that you describe. Then supplement it with something like the following to handle any cases where instances get created for you (i.e. array controllers):

    - (void)willSave
    {
        if ([self name] == nil) {
            NSManagedObjectContext *context = [self managedObjectContext];
            Name *name = [NSEntityDescription insertNewObjectForEntityForName:@"Name" inManagedObjectContext:context];
            [self setName:name];
        }
    }
    

    ...and of course don't bother with a custom awakeFromInsert...

提交回复
热议问题