简体   繁体   English

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

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

I have the following 2 Entity's in my xcdatamodel:我的 xcdatamodel 中有以下 2 个实体:

  1. Matrix 1.1 Attributes Name 1.2 Relationships MatrixToProcess The Destination is: Process The Inverse is: ProcessToMatrix The To-Many Relationship is checked The Delete Rule is Cascade Matrix 1.1 属性名称 1.2 关系 MatrixToProcess 目标是:Process 逆是:ProcessToMatrix 选中对多关系 删除规则是级联

  2. Process 2.1 Attributes Name 2.2 Relationships ProcessToMatrix The Destination is: Matrix The Inverse is: MatrixToProcess The To-Many Relationship is not checked The Delete Rule is Nullify过程 2.1 属性名称 2.2 关系 ProcessToMatrix 目标是:矩阵 逆是:MatrixToProcess 不检查多对多关系 删除规则为 Nullify

I can successfully add a new Matrix, which is added and shows up correctly in the UITableView.我可以成功添加一个新的矩阵,该矩阵已添加并正确显示在 UITableView 中。

I can successfully add a new Process, however, All necessary information is added to the database with the EXCEPTION of the Z_PK value from the Matrix table.我可以成功添加一个新进程,但是,所有必要的信息都添加到数据库中,但矩阵表中的 Z_PK 值除外。 ie The sqlite database in the iPhone simulator will create the new Process Name, but does NOT enter any information into the ZPROCESSTOMATRIX column.即 iPhone 模拟器中的 sqlite 数据库将创建新的进程名称,但不会在 ZPROCESSTOMATRIX 列中输入任何信息。 If I manually insert the associated Matrix Name Z_PK value, everything works.如果我手动插入关联的矩阵名称 Z_PK 值,一切正常。

I am struggling with.我在挣扎。 don't fully understand how to add the addObject under the Process *newProcess = [NSEntityDescription insertNewObjectForEntityForName:@"Process" inManagedObjectContext:self.managedObjectContext];.不完全理解如何在 Process *newProcess = [NSEntityDescription insertNewObjectForEntityForName:@"Process" inManagedObjectContext:self.managedObjectContext]; 下添加 addObject。 This is the code I am using:这是我正在使用的代码:

- (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];
}

Any help and/or direction will be greatly appreciated.任何帮助和/或方向将不胜感激。

You need to set the relationship between the new Process and the Matrix.您需要设置新流程和矩阵之间的关系。

Check out this answer to iphone core data inserting new objects question .查看iphone 核心数据插入新对象问题的回答

UPDATE更新

Assuming that you have generated the class files for Process and Matrix AND Process has a relationship to Matrix named "processToMatrix", the code to set the relationship would be:假设您已经为 Process 和 Matrix 生成了类文件,并且 Process 与名为“processToMatrix”的 Matrix 有关系,则设置关系的代码将是:

newProcess.processToMatrix = matrix;

where "matrix" the the Matrix object that should be associated with the new Process object.其中“矩阵”是应与新 Process 对象关联的 Matrix 对象。

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

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