Help passing managedObjectContext through segue

I have a simple app that I am developing. It stores one line to do list items in a core data model. I am completely new to core data. What I am trying to do is pass my managedObjectContext from my main viewcontroller to my second viewcontroller.


This is the prepareforsegue that I have:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"EditItemSegue"])
    {
        NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
        Item *item = [[self fetchedResultsController]objectAtIndexPath:indexPath];
        EditItem *destination = (EditItem *)segue.destinationViewController;
        destination.managedObjectContext=self.managedObjectContext;
        destination.toDoItemName = item;
    }
}


Question 1: Is this correct?

Question 2: On my second viewcontroller, how do I grab the moc? I now how it works if I am using SQLite but I think I am getting things confused.


Also


destination.toDoItemName = item;


This line complains: incompatible pointer types assigning nsstring to item

I know what this means but I am not completely sure how to resolve it in this situation.


My source is here: https://github.com/martylavender/LittleToDoApp/tree/addingcoredata


Any help with this would be very much appreciated.

Answer to Question 1: You are doing the right thing in your code.

Answer to Question 2: In your second viewcontroller, you can use it like this.

[self.managedObjectContext save:nil]

The code below is failing because you declared toDoItemName to be an NSString.

destination.toDoItemName = item;


You should be passing the name like the sample below.


destination.toDoItemName = item.name;
Help passing managedObjectContext through segue
 
 
Q