一直在尝试与核心数据建立一对多关系,但进展甚微。
只是试图做一个简单的核心数据示例。
实体 A = 类型:typeName = 字符串。
实体 B = 例程:routineName = 字符串。
关系:类型有很多套路。
我有几个视图控制器。
TypeViewController(表格视图控制器,用于列出来自实体 A 的所有类型。
AddTypeViewController(视图控制器通过 typeTextField 向实体添加类型。这是从 TypeViewController 中的添加按钮推送的)
RoutineViewController(从 TypeViewController 中选择类型时推送的表视图控制器。列出实体 B 中与所选类型关联的例程。
AddRoutineViewController(视图控制器通过routineNameTextField将Routine添加到实体B。这是从RoutineView Controller中的添加按钮推送的。
我开始只实现第一个实体并设法让前 2 个视图控制器工作。(能够添加类型并在第一个视图控制器上查看它们。
一旦我创建了第二个实体并链接了关系(也是反向关系),第一部分就停止了工作。
问:我是否必须更改在视图控制器 1 中获取数据的方式?(在这个视图控制器中,我只从实体 A 获取数据)。
Q. 可以单独添加数据吗?(从视图控制器 2 添加类型数据。从视图控制器 4 添加例程数据。)
Q. 在视图控制器 4 中保存数据时...
- (IBAction)savePressed:(id)sender {
RoutinePlan *routinePlan = [[RoutinePlan alloc] initWithEntity:[NSEntityDescription entityForName:@"RoutinePlan" inManagedObjectContext:self.managedObjectContext]
insertIntoManagedObjectContext:self.managedObjectContext];
[routinePlan setValue:self.rNameTextField.text forKey:@"rName"];
**How to add this routine to the selected type**
NSError *savingError;
if (![self.managedObjectContext save:&savingError])NSLog(@"Error saving: %@", savingError);
}
感谢您的任何帮助。如果需要更多信息,我可以提供...
还有人知道一对多关系的基本示例吗?
编辑:
***来自 TypeViewController.m 的代码片段
- (NSManagedObjectContext *)managedObjectContext{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"current Type Selected"]) {
NSManagedObject *selectedRoutine = [self.routineTypeArray objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
RoutinePlanViewController *destViewController = segue.destinationViewController;
destViewController.routineTypeObject = selectedRoutine;
}}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
// Fetch the devices from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Type"];
self.routineTypeArray = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tableView reloadData];}
**来自 AddTypeViewController.m 的代码片段
- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;}
- (IBAction)savePressed:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *newRoutine = [NSEntityDescription insertNewObjectForEntityForName:@"Type" inManagedObjectContext:context];
[newRoutine setValue:self.rTypeTextField.text forKey:@"rType"];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
[self dismissViewControllerAnimated:YES completion:nil]; }
**来自 RoutineViewController.m 的代码片段
@synthesize routineTypeObject;
- (NSManagedObjectContext *)managedObjectContext{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"current Type Selected"]) {
AddRoutineViewController *destViewController = segue.destinationViewController;
destViewController.routineTypeObject = routineTypeObject;
}
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Fetch the devices from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Routine"];
self.routineArray = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tableView reloadData];
}
**来自 AddRoutineViewController.m 的代码片段
@synthesize routineTypeObject;
- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (IBAction)savePressed:(id)sender {
Routine *routinePlan = [[Routine alloc] initWithEntity:[NSEntityDescription entityForName:@"Routine" inManagedObjectContext:self.managedObjectContext]
insertIntoManagedObjectContext:self.managedObjectContext];
[routinePlan setValue:self.rNameTextField.text forKey:@"rName"];
**Error
[Routine.Type = routineTypeObject];
NSError *savingError;
if (![self.managedObjectContext save:&savingError])NSLog(@"Error saving: %@", savingError);
}