Core data 核心数据父子关系

Core data 核心数据父子关系,core-data,Core Data,我有一个关于核心数据的基本问题 我有两张一对多的桌子 我已将应用程序设置为将子项添加到父项,但我无法理解如何设置关系,以便在通过视图控制器添加新子项时,它将子项添加到正确的父项 - (IBAction)save:(id)sender { NSManagedObjectContext *context = [self managedObjectContext]; Child *newChild = [NSEntityDescription insertNewObjectForEntityF

我有一个关于核心数据的基本问题

我有两张一对多的桌子

我已将应用程序设置为将子项添加到父项,但我无法理解如何设置关系,以便在通过视图控制器添加新子项时,它将子项添加到正确的父项

 - (IBAction)save:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext]; 

 Child *newChild = [NSEntityDescription insertNewObjectForEntityForName:@"Child" inManagedObjectContext:context];
    [newChild setValue:self.childName.text forKey:@"childName"];
    [newChild setValue:self.born.text forKey:@"born"];


    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ParentList" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSError *error = nil;
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

    ParentList *parent = [fetchedObjects objectAtIndex:0]; //this adds it to the first parentList in list at index 0 not to the correct parent
    NSLog(@"parent: %@ created", league);
    [parent addChildObject: newChild];

        //
        ///////////////////////////////////////////
        //////index path is wrong//////////////////
        ///////////////////////////////////////////



}


NSError *error = nil;
    // Save the object to persistent store
if (![context save:&error]) {
    NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}

[self dismissViewControllerAnimated:YES completion:nil];
我已经生成了实体子类,并设法让应用程序添加一个子类(但它会将其添加到索引0),但我似乎无法处理fetchrequest以找到正确的父类

 - (IBAction)save:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext]; 

 Child *newChild = [NSEntityDescription insertNewObjectForEntityForName:@"Child" inManagedObjectContext:context];
    [newChild setValue:self.childName.text forKey:@"childName"];
    [newChild setValue:self.born.text forKey:@"born"];


    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ParentList" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSError *error = nil;
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

    ParentList *parent = [fetchedObjects objectAtIndex:0]; //this adds it to the first parentList in list at index 0 not to the correct parent
    NSLog(@"parent: %@ created", league);
    [parent addChildObject: newChild];

        //
        ///////////////////////////////////////////
        //////index path is wrong//////////////////
        ///////////////////////////////////////////



}


NSError *error = nil;
    // Save the object to persistent store
if (![context save:&error]) {
    NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}

[self dismissViewControllerAnimated:YES completion:nil];

}

您需要将家长的
objectID
传递给第二视图控制器(如果我正确理解您的设置)。
在其他视图上下文中获取父对象(使用NSManagedObjectContext的
existingObjectWithID:error:

将子父对象设置为获取的对象。

应该看起来像:

NSError* error = nil;
NSManagedObjectID* parentID = //the parent object id you selected
Parent* parent = [context existingObjectWithID:parentID error:&error];
if (parent) { //parent exists
    Child *newChild = [NSEntityDescription insertNewObjectForEntityForName:@"Child" 
                                                    inManagedObjectContext:context];
    [newChild setValue:self.childName.text forKey:@"childName"];
    [newChild setValue:self.born.text forKey:@"born"];
    [newChild setValue:parent forKey:@"parent"];//Set the parent
} else {
    NSLog(@"ERROR:: error fetching parent: %@",error);
}
编辑:

获取所选对象id(假设您使用的是
nsfetchedreaultscocontroller
):


我以前在阅读教程时遇到过ObjectID,但我显然需要阅读和学习更多。为了进一步解释,我有两个表视图,每个表视图都有一个输入视图控制器,用于添加父文本和子文本。我可以将数据输入到父级,但我需要找到在父级上选择的ObjectID,并将其传递给子表视图控制器,然后将其传递给子文本条目视图控制器。您能解释一下我是如何从父选定行获取objectID并通过prepareforsegue传递它的吗?