21 Feb 2010, 23:44
Generic-user-small

Jennifer Rosamond (3 posts)

I have a navigation based iphone project using Core Data. In all of the examples I’ve seen and in the XCode template, the rootviewcontroller loads all the data. However, in my app the rootviewcontroller is just a three item menu so when the user touches a menu item, a table view is loaded that needs to access data. I have a few questions about this:
1. In the appDelegate’s applicationDidFinishLaunching: instead of rootViewController.managedObjectContext = self.managedObjectContext; Would I set the context of my secondary tableview controller to the appDelegate’s context?
2. Can I just cut the fetch methods that the template puts in the rootviewcontroller.m and paste them into my secondary tableview controller?
3. Since the rootviewcontroller is just a menu and doesn’t need to access the database, does it even need any references to the context?

22 Feb 2010, 16:47
Avatar_pragsmall

Marcus S. Zarra (239 posts)

I would have your root view controller hand off the NSManagedObjectContext to the next view controller. This is the dependency injection design pattern and will make your code more flexible.

NSFetchedResultsController instances are one-to-one with UITableViewController instances. Therefore, if your second UITableViewController needs a NSFetchedResultsController you should create a new one and you can definitely copy the code from the root for the second controller.

While the root may not need access to the NSManagedObjectContext it controls the next level of view controllers, therefore it needs it for that function.

22 Feb 2010, 21:15
Generic-user-small

Jennifer Rosamond (3 posts)

Thanks Marcus. I have one other question. In the case of the application I described above, would it be better to transfer the Core Data Stack methods that the template places in the appDelegate to the root view controller? Or is this code required to be in the appDelegate? I apologize if these are stupid questions. Even with the book and the apple documentation, I am having a difficult time applying Core Data to this app.

23 Feb 2010, 15:47
Avatar_pragsmall

Marcus S. Zarra (239 posts)

There is never a stupid question, just unanswered ones :)

I would recommend leaving it in the app delegate so that you can:

1. build up and tear down view controllers safely;
2. you can save the persistent store on exit of the application

  You must be logged in to comment