在关系字段中插入值

时间:2011-11-03 18:24:47

标签: iphone ios core-data entity-relationship ios5

我试图在核心数据实体中插入值,但是当我尝试在关系字段中插入它时崩溃

NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
        NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:      [entity name] inManagedObjectContext:context];
        [newManagedObject setValue:[NSDate date] forKey:@"fechaAprovacion"];
        [newManagedObject setValue:[NSDate date] forKey:@"fechaUltimoOficio"];
 //relationship field----->[newManagedObject setValue:[NSNumber numberWithInteger:self.index.row] forKey:@"vObra"]; //<---relationship field

        // Save the context.
        NSError *error = nil;
        if (![context save:&error]) { 
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }

1 个答案:

答案 0 :(得分:0)

我解决了!!我只需要插入与此相关的NSManagedObject

NSManagedObject *reporteManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Reporte" inManagedObjectContext:self.managedObjectContext];

NSManagedObject *partidaManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Avance" inManagedObjectContext:self.managedObjectContext];

[partidaManagedObject setValue:reporteManagedObject forKey:@"avanceReporte"];