Note that there are some explanatory texts on larger screens.

plurals
  1. POCore Data One to Many Simple Example
    primarykey
    data
    text
    <p>Been trying to create a One to Many relationship with core data with little progress.</p> <p>Just trying to make a simple core data example.</p> <p>Entity A = Type: typeName = String.</p> <p>Entity B = Routine: routineName = String.</p> <p>Relationship: Type has many routines.</p> <p>Ive got a few view controllers.</p> <ol> <li><p>TypeViewController (Table view controller to list all the Types from Entity A. </p></li> <li><p>AddTypeViewController (View controller to add a type to the entity through a typeTextField. This is pushed from a add button in the TypeViewController)</p></li> <li><p>RoutineViewController (Table view controller which is pushed when a type is selected from TypeViewController. Lists the routines from Entity B associated with the type selected.</p></li> <li><p>AddRoutineViewController (View controller to add Routine to entity B through routineNameTextField. This is pushed from a add button in the RoutineView Controller.</p></li> </ol> <p>I started off with implementing just the first Entity and managed to get the First 2 view controllers working. (Was able to add types and view them on the first view controller.</p> <p>Once i created the second entity and linked the relationship (also the inverse relationship too) the first part stopped working.</p> <p>Q. Do i have to change the way data is fetched in view controller 1? (As in this view controller i am only fetching data from Entity A).</p> <p>Q. Is it possible to be adding data separately? (adding type data from the view controller 2. and adding routine data from view controller 4.)</p> <p>Q. When saving data in view controller 4...</p> <pre><code>- (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:&amp;savingError])NSLog(@"Error saving: %@", savingError); } </code></pre> <p>Thank you for any help. If any more information is needed i can provide it...</p> <p>Also does anyone know of a basic example out there of one to many relationships?</p> <p>EDIT:</p> <p>***Code snippet from TypeViewController.m</p> <pre><code>- (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];} </code></pre> <p>**Code Snippet from AddTypeViewController.m</p> <pre><code>- (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:&amp;error]) { NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]); } [self dismissViewControllerAnimated:YES completion:nil]; } </code></pre> <p>**Code Snippet from RoutineViewController.m</p> <pre><code>@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]; } </code></pre> <p>**Code Snippet from AddRoutineViewController.m</p> <pre><code>@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:&amp;savingError])NSLog(@"Error saving: %@", savingError); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload