title: Core Data Not Saving To Table View
body: <p>So this is my first time working with core data, and so far it hasn't been the best experience. My application so far consists of two UITableView controllers and a single ViewController. The app simply asks the user to enter the name of a list on UIAlert and it saves to core data, and the name of the list is put into the first tableview. Then the user clicks on the name of the list and it pushes them to the contents on the list, which is empty because it hasn't been populated yet. Then the user populates the TableView, so it pushes to the single view controller where you would enter all the info and hit save. My problem is it doesn't save when I hit save, it goes back to the last UITableView Controller and nothing is there. Thats my first problem, my second is I would like to pass the data between views so when the user clicks on a list it pushes to the second UITableView Controller and says the name of the list at the top. I'm getting really confused with all the core data stuff and relationships so if someone could help me out I would appreciate it. I'll include my code and data model.</p>
  
  <p>Data Model</p>
  
  <p><img src="https://i.stack.imgur.com/sY5DB.png" alt="enter image description here">
  First Views .m (view that lists all the lists) </p>
  
  <pre><code>- (NSManagedObjectContext *)managedObjectContext {
      NSManagedObjectContext *context = nil;
      id delegate = [[UIApplication sharedApplication] delegate];
      if ([delegate respondsToSelector:@selector(managedObjectContext)]) {
          context = [delegate managedObjectContext];
      }
      return context;
  }
  
  - (void)viewDidAppear:(BOOL)animated
  {
      [super viewDidAppear:animated];
  
      // Fetch the lists from persistent data store
      NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
      NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"List"];
      self.lists = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
      [self.tableView reloadData];
  }
  
  - (id)initWithStyle:(UITableViewStyle)style
  {
      self = [super initWithStyle:style];
      if (self) {
          // Custom initialization
      }
      return self;
  }
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
  
      // Uncomment the following line to preserve selection between presentations.
      // self.clearsSelectionOnViewWillAppear = NO;
  
      // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
      // self.navigationItem.rightBarButtonItem = self.editButtonItem;
  }
  
  - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
  }
  
  #pragma mark - Table view data source
  
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
      // Return the number of sections.
      return 1;
  }
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
      // Return the number of rows in the section.
      return self.lists.count;
  }
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  
      // Configure the cell...
      NSManagedObject *list = [self.lists objectAtIndex:indexPath.row];
      [cell.textLabel setText:[list valueForKey:@"name"]];
  
      return cell;
  }
  
  -(IBAction)add:(id)sender {
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add List" message:@"Create a New Wish List" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Save", nil];
      [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
      [alert setTag:2];
      [alert show];
      alert.delegate = self;
  }
  - (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  {
      if (buttonIndex != 0 &amp;&amp; alertView.tag == 2) {
          UITextField *tf = [alertView textFieldAtIndex:0];
          NSManagedObjectContext *context = [self managedObjectContext];
  
          // Create a new managed object
          NSManagedObject *newList = [NSEntityDescription insertNewObjectForEntityForName:@"List" inManagedObjectContext:context];
          [newList setValue:tf.text forKey:@"name"];
  
          NSError *error = nil;
          // Save the object to persistent store
          if (![context save:&amp;error]) {
              NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
  
  
          }
  
          [self dismissViewControllerAnimated:YES completion:nil];
      }
  
      NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
      NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"List"];
      self.lists = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
  
      [self.tableView reloadData];
  
  }
  - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  {
      // Return NO if you do not want the specified item to be editable.
      return YES;
  }
  
  
  - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  {
      NSManagedObjectContext *context = [self managedObjectContext];
  
      if (editingStyle == UITableViewCellEditingStyleDelete) {
          // Delete object from database
          [context deleteObject:[self.lists objectAtIndex:indexPath.row]];
  
          NSError *error = nil;
          if (![context save:&amp;error]) {
              NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
              return;
          }
  
          // Remove list from table view
          [self.lists removeObjectAtIndex:indexPath.row];
          [self.items removeObjectAtIndex:indexPath.row];
          [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
      }
  }
  @end
  </code></pre>
  
  <p>Second view's .m (view that displays all items in list)</p>
  
  <pre><code>- (NSManagedObjectContext *)managedObjectContext {
      NSManagedObjectContext *context = nil;
      id delegate = [[UIApplication sharedApplication] delegate];
      if ([delegate respondsToSelector:@selector(managedObjectContext)]) {
          context = [delegate managedObjectContext];
      }
      return context;
  }
  
  - (void)viewDidAppear:(BOOL)animated
  {
      [super viewDidAppear:animated];
  
      // Fetch the lists from persistent data store
      NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
      NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Item"];
      self.lists = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
      [self.tableView reloadData];
  }
  
  - (id)initWithStyle:(UITableViewStyle)style
  {
      self = [super initWithStyle:style];
      if (self) {
          // Custom initialization
      }
      return self;
  }
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
  
  
      // Uncomment the following line to preserve selection between presentations.
      // self.clearsSelectionOnViewWillAppear = NO;
  
      // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
      // self.navigationItem.rightBarButtonItem = self.editButtonItem;
  }
  
  - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
  }
  
  #pragma mark - Table view data source
  
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
      // Return the number of sections.
      return 1;
  }
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
      // Return the number of rows in the section.
      return self.items.count;
  }
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  
      // Configure the cell...
      NSManagedObject *item = [self.items objectAtIndex:indexPath.row];
      [cell.textLabel setText:[item valueForKey:@"list"]];
  
      return cell;
  }
  
  
  - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  {
      // Return NO if you do not want the specified item to be editable.
      return YES;
  }
  
  
  - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  {
      NSManagedObjectContext *context = [self managedObjectContext];
  
      if (editingStyle == UITableViewCellEditingStyleDelete) {
          // Delete object from database
          [context deleteObject:[self.items objectAtIndex:indexPath.row]];
  
          NSError *error = nil;
          if (![context save:&amp;error]) {
              NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
              return;
          }
  
          // Remove device from table view
          [self.items removeObjectAtIndex:indexPath.row];
          [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
      }
  }
  
  
  
  #pragma mark - Table view delegate
  
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
      // Navigation logic may go here. Create and push another view controller.
      /*
       &lt;#DetailViewController#&gt; *detailViewController = [[&lt;#DetailViewController#&gt; alloc] initWithNibName:@"&lt;#Nib name#&gt;" bundle:nil];
       // ...
       // Pass the selected object to the new view controller.
       [self.navigationController pushViewController:detailViewController animated:YES];
       */
  }
  
  
  #pragma mark - Segue
  
  - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  {
      if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
          NSManagedObject *selectedItem = [self.items objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
          AddViewController *destViewController = segue.destinationViewController;
          destViewController.items = selectedItem;
      }
  }
  
  @end
  </code></pre>
  
  <p>And last view (view that adds items to list) </p>
  
  <pre><code>- (NSManagedObjectContext *)managedObjectContext {
      NSManagedObjectContext *context = nil;
      id delegate = [[UIApplication sharedApplication] delegate];
      if ([delegate respondsToSelector:@selector(managedObjectContext)]) {
          context = [delegate managedObjectContext];
      }
      return context;
  }
  
  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
          // Custom initialization
      }
      return self;
  }
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
  
  }
  
  - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
  }
  
  - (IBAction)cancel:(id)sender {
      [self dismissViewControllerAnimated:YES completion:nil];
  }
  
  - (IBAction)save:(id)sender {
      NSManagedObjectContext *context = [self managedObjectContext];
  
      // Create a new managed object
      NSManagedObject *newItem = [NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:context];
      [newItem setValue:self.lists forKey:@"list"];
      [newItem setValue:self.lists forKey:@"list"];
      [newItem setValue:self.lists forKey:@"list"];
  
  
      NSError *error = nil;
      // Save the object to persistent store
      if (![context save:&amp;error]) {
          NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
      }
  
      [self dismissViewControllerAnimated:YES completion:nil];
  }
  @end
  </code></pre>
  
  <p>In my model view that allows me to create a new item:</p>
  
  <pre><code>[newItem setValue:self.name.text forKey:@"itemName"];
  [newItem setValue:self.price.text forKey:@"price"];
  [newItem setValue:self.desc.text forKey:@"description"];
  </code></pre>
  
  <p>Error:</p>
  
  <p>self=(AddViewController *)0x8d22e90
  newItem=(Item_Item_ *)0x8a636b0</p>