插入实体时的核心数据获取保存关系(同时)

时间:2013-04-22 00:46:01

标签: core-data insert many-to-many entity relationships

这里的第一个问题,我尝试了很多东西,无法弄清楚 具有2个实体的核心数据,双向关系为多对

A<<---->>B
A实体将名称作为属性,并将关系Bs

第一个控制器列出了所有A个实体,我有第二个控制器来添加A个实体,我想让它在其关系中保存一个默认的B

在准备segue时我有这段代码:

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;

}

在addAntityViewController中我有这个保存

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

addAEntityViewController中,即使NSLog(@"A = %@", self.A.Bs);语句显示B,一切也能正确保存。但是当代理人保存在第一个控制器(AEntityViewController)中时,它只保存A.name而不保存A.Bs关系,我无法弄清楚是什么问题。

这是委托方法:

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

就像我说的那样保存了A实体而不是与B的关系,即使与B的关系在addAEntityViewController(第二个视图)中正确保存了。 NSLOg的{​​{1}}为空。

1 个答案:

答案 0 :(得分:0)

首先我相信这一行:
addAEntityViewController = self.managedObjectContext;
应该是:
addAEntityViewController.managedObjectContext = self.managedObjectContext;

但这也是错误的 它应该是你之后创建的addingContext
addAEntityViewController.managedObjectContext = addingContext;

我对您的应用程序没有崩溃感到有点惊讶,因为您正在混合来自两个不同上下文的托管对象。