【问题标题】:Core data parent child relationship核心数据父子关系
【发布时间】:2013-04-26 17:02:38
【问题描述】:

我有一个关于 Core Data 的基本问题。

我有两张一对多的桌子。

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

我已经生成了实体子类并设法让应用程序添加一个子类(但它会将它添加到索引 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];

}

【问题讨论】:

    标签: core-data


    【解决方案1】:

    您需要将父级的 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(假设您使用的是 NSFetchedReaultsController):

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
        //Use object.objectID as the selected object id to pass to the other view controller
        //what ever you need to do with the object
    }
    

    【讨论】:

    • 之前在看教程的时候也遇到过ObjectID,但显然需要多看多学习。为了解释更多,我有 2 个表视图,每个表视图都有一个输入视图控制器,用于添加父文本和子文本。我可以将数据输入到父级,但我需要找到在父级上选择的 ObjectID 并将其传递给子表视图控制器,然后将其传递给子文本输入视图控制器。你能解释一下我如何从父选择的行中获取 objectID 并通过 prepareforsegue 传递它吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多