0
votes

First question here and I've tried a bunch of stuff and can't figure it out.
Core Data with 2 entities with to-many relationship both ways

A<<---->>B
A entity has name as an attribute, and a relationship Bs

First Controller lists all A entities and i have a second controller to add A entities and I want to have it save a default B in its relationship.

In the prepare for segue I have this code:

if ([[segue identifier] isEqualToString:@"addAEntitySegue"]) {

    AddAEntityViewController *addAEntityViewController = [segue destinationViewController];
    addAEntityViewController.delegate = self;
    addAEntityViewController.managedObjectContext = self.managedObjectContext;

    NSManagedObjectContext *addingContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    self.addingManagedObjectContext = addingContext;

    [addingManagedObjectContext setPersistentStoreCoordinator:[[fetchedResultsController managedObjectContext] persistentStoreCoordinator]];

   addAEntityViewController.A = [NSEntityDescription insertNewObjectForEntityForName:@"A" inManagedObjectContext:addingContext];

    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
                                                                           target:addAEntityViewController
                                                                               action:@selector(save:)];
    addAEntityViewController.navigationItem.rightBarButtonItem = saveButton;

}

In addAEntityViewController i have this to save

-(IBAction)save:(id)sender
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"B"                                                    inManagedObjectContext:self.managedObjectContext];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@",[defaults objectForKey:@"BDefault"]];

    [fetchRequest setEntity:entity];
    [fetchRequest setPredicate:predicate];

     //Set the batch size to a suitable number
    [fetchRequest setFetchBatchSize:20];
    NSError *error;

    self.A.name = textFieldVal;

    [self.A addBObject:[[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] objectAtIndex:0]];

    NSLog(@"A = %@", self.A.Bs);

    [self.delegate addAEntityViewController:self didFinishWithSave:YES];
}

In the addAEntityViewController everything saves correctly even the NSLog(@"A = %@", self.A.Bs); statement shows the B. But when the delegate saves in the First Controller (AEntityViewController) it only saves the A.name but not the relationship A.Bs, I can't figure out what's wrong.

Here's the delegate method:

-(void) addAEntityViewController:self didFinishWithSave:YES{
if (save) {
    NSLog(@"saveworkouts");

    NSError *error;
    if (![addingManagedObjectContext save:&error]) {
        // Update to handle the error appropriately.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    }
}

// release the adding managed object context
self.addingManagedObjectContext = nil;
}

Like I said it saves the A entity but not the relationship to B even though the relationship to B saved correctly in the addAEntityViewController (the second View). An NSLOg of A.Bs is null.

1

1 Answers

0
votes

First I believe that this line:
addAEntityViewController = self.managedObjectContext;
should be:
addAEntityViewController.managedObjectContext = self.managedObjectContext;

but that would also be wrong.
it should be getting the addingContext you created afterwards:
addAEntityViewController.managedObjectContext = addingContext;

I'm a bit surprised that your app didn't crash, as you are mixing managed objects from 2 different contexts.