views:

21

answers:

1

Hi All,

I've got a program which is working and uses Core Data for storage.

However, I'm not entirely sure if I should be keeping my fetch/update methods exclusively to the app delegate?

One example is for a question within the app. The question has a "left" and a "right" statement.

The delegate currently creates the new QuestionVC and passes in the questionNumber for the question. Then the QuestionVC then does a fetch to get the question object and uses the left and right properties of the object to set the text on the screen.

Should I do this the other way round...

The delegate does a fetch on the question number and then creates the QuestionVC and passes in the question object. The QuestionVC then just has to get the left and right text without having to do a fetch at all.

Any tips, advice welcome.

Thanks

Oliver

+2  A: 

Both approaches sound valid, but if you can design your view controller hierarchy in such a way that only one object needs to know about Core Data (i.e. pass the question object to your QuestionVC) then that's probably a simpler design, which is probably better.

I personally wouldn't be doing any fetching in my app delegate, though. My app delegates only set up Core Data (i.e. the managed object context) and pass that to the root view controller. I prefer to keep my app delegates as small as possible. I don't use them as an all-purpose singleton.

Shaggy Frog
That is sound advice in IMHO. The `AppDelegate` should be *tiny* and not a dumping ground for shared code.
Marcus S. Zarra
Praise from (Core Data) Caesar! :)
Shaggy Frog
Thanks for your answer! It makes a lot of sense.Would you happen to have a link to a reference about programming with the app delegate? Mine is getting quite large and I'm worried about it becoming too large and unmanageable.I suppose a better method would be to create a class to manage the questions and use that to fetch the questions and push/pop the QuestionVC to the view (would this be done via the appdeegate? i.e. [[delegate navController] pushViewController:questionVC animated:YES];)This would allow me to minimise the imports and the number of methods within the delegate?
Fogmeister
You could create a "data model" class using the proxy pattern to fetch your questions, instantiating it in your app delegate and then handing it off to your view controllers. Or you could create a custom view controller class to be your "root" view controller, and have it initialize your set of questions from Core Data (perhaps using `NSFetchedResultsController`).
Shaggy Frog
Thanks again for the help! I have started to restructure it a bit now. I already feel better about it as I can see how it is already more manageable!Thanks!
Fogmeister