How do I display core data on second view controller?

Posted by jon on Stack Overflow See other posts from Stack Overflow or by jon
Published on 2010-10-05T04:22:49Z Indexed on 2010/12/23 23:54 UTC
Read the original article Hit count: 155

Filed under:
|
|

I am working on my first core data iPhone application. I am using a navigation controller, and the root view controller displays 4 rows. Clicking the first row takes me to a second table view controller. However, when I click the back button, repeat the row tap, click the back button again, and tap the row a third time, I get an error. I have been researching this for a week with no success.

I can reproduce the error easily:

  1. Create a new Navigation-based Application, use Core Data for storage, call it MyTest which creates MyTestAppDelegate and RootViewController.
  2. Add new UIViewController subclass, with UITableViewController and xib, call it ListViewController.
  3. Copy code from RootViewController.h and .m to ListViewController.h and .m., changing the file names appropriately. To simplify the code, I removed the trailing “_” from all variables.
  4. In RootViewController, I added #import ListViewController.h, set up an array to display 4 rows and navigate to ListViewController when clicking the first row.

In ListViewController.m, I added #import MyTestAppDelegate.h” and the following code:

- (void)viewDidLoad {
    [super viewDidLoad];

 if (managedObjectContext == nil) {
        managedObjectContext = [(MyTestAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
 }
    ..
}

The sequence that causes the error is tap row, return, tap row, return, tap row -> error. managedObjectContext is synthesized for the third time. I appreciate your patience and your help, as this makes no sense to me.

ADDENDUM: I may have a partial solution. http://www.iphonedevsdk.com/forum/iphone-sdk-development/41688-accessing-app-delegates-managed-object-context.html

If I do not release the managedObjectContext in the .m file, the error goes away. Is that ok or will that cause me issues?

 - (void)dealloc {
    [fetchedResultsController release];
    // [managedObjectContext release];
    [super dealloc];
}

ADDENDUM 2: See solution below. Sorry for the formatting issues - this was my first post.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about xcode