繁体   English   中英

用于 iPhone 核心数据中一对多关系的 addObject

[英]addObject for one/many-to-many relationships in core data for iPhone

我的 xcdatamodel 中有以下 2 个实体:

  1. Matrix 1.1 属性名称 1.2 关系 MatrixToProcess 目标是:Process 逆是:ProcessToMatrix 选中对多关系 删除规则是级联

  2. 过程 2.1 属性名称 2.2 关系 ProcessToMatrix 目标是:矩阵 逆是:MatrixToProcess 不检查多对多关系 删除规则为 Nullify

我可以成功添加一个新的矩阵,该矩阵已添加并正确显示在 UITableView 中。

我可以成功添加一个新进程,但是,所有必要的信息都添加到数据库中,但矩阵表中的 Z_PK 值除外。 即 iPhone 模拟器中的 sqlite 数据库将创建新的进程名称,但不会在 ZPROCESSTOMATRIX 列中输入任何信息。 如果我手动插入关联的矩阵名称 Z_PK 值,一切正常。

我在挣扎。 不完全理解如何在 Process *newProcess = [NSEntityDescription insertNewObjectForEntityForName:@"Process" inManagedObjectContext:self.managedObjectContext]; 下添加 addObject。 这是我正在使用的代码:

- (void)addProcess:(id)sender {
    ProcessAddViewController *addController = [[ProcessAddViewController alloc] initWithNibName:@"ProcessAddView" bundle:nil];
    addController.delegate = self;  
    Process *newProcess = [NSEntityDescription insertNewObjectForEntityForName:@"Process" inManagedObjectContext:self.managedObjectContext];
    addController.process = newProcess;
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addController];
    [self presentModalViewController:navigationController animated:YES];    
    [navigationController release];
    [addController release];
}

- (void)processAddViewController:(ProcessAddViewController *)processAddViewController didAddProcess:(Process *)process {
    if (process) {        
        [self showProcess:process animated:NO];
    }

    [self dismissModalViewControllerAnimated:YES];
}


- (void)showProcess:(Process *)process animated:(BOOL)animated {
    RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStyleGrouped];
    rootViewController.managedObjectContext = self.managedObjectContext;    
    if(entitySearchPredicate == nil)
    {
        NSMutableArray* mutableFetchResults = [CoreDataHelper getObjectsFromContext:self.entityName :@"displayOrder" :YES :managedObjectContext];       
        [self setEntityArray:mutableFetchResults];
        [mutableFetchResults release];
    }
    else
    {
        NSMutableArray* mutableFetchResults = [CoreDataHelper searchObjectsInContext:self.entityName :entitySearchPredicate :@"displayOrder" :YES :managedObjectContext];
        [self setEntityArray:mutableFetchResults];
        [mutableFetchResults release];
    }
    [rootViewController release];
}

任何帮助和/或方向将不胜感激。

您需要设置新流程和矩阵之间的关系。

查看iphone 核心数据插入新对象问题的回答

更新

假设您已经为 Process 和 Matrix 生成了类文件,并且 Process 与名为“processToMatrix”的 Matrix 有关系,则设置关系的代码将是:

newProcess.processToMatrix = matrix;

其中“矩阵”是应与新 Process 对象关联的 Matrix 对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM