简体   繁体   中英

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

I have the following 2 Entity's in my xcdatamodel:

  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

  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

I can successfully add a new Matrix, which is added and shows up correctly in the 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. ie The sqlite database in the iPhone simulator will create the new Process Name, but does NOT enter any information into the ZPROCESSTOMATRIX column. If I manually insert the associated Matrix Name Z_PK value, everything works.

I am struggling with. don't fully understand how to add the addObject under the Process *newProcess = [NSEntityDescription insertNewObjectForEntityForName:@"Process" inManagedObjectContext:self.managedObjectContext];. 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 .

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:

newProcess.processToMatrix = matrix;

where "matrix" the the Matrix object that should be associated with the new Process object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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