views:

853

answers:

2

Hi

Im building an app that will use a Core Data model. I pretty new at Objective C and my usual design patterns does not really apply on Core Data and Objective C, at least I can't seem to find examples that confirms they will.

I have been through the Apple Developer examples and different sources on the intertubes.

It seems that to leverage Core Data I need to pass the managedObjectContext to each of my viewControllers, have the viewController implement the NSFetchedResultsControllerDelegate and then implement each of the methods for doing a fetch and subsequently implement

NSFetchedResultsChangeInsert

NSFetchedResultsChangeDelete NSFetchedResultsChangeMove NSFetchedResultsChangeUpdate

This adds approximately 100+ lines of code in each viewController and it is 90% the same code I write again and again. Plus I have to pass everything around and keep track of it's memory footprint.

In other languages I would build a singleton model of a few classes that held methods for maintaining and delivering data upon request, available from anywhere. It seems I can't take that approach in Objective C. If I where to build a static Class that took a managedObjectContext and returned me what I needed, I would still have to pass the managedObjectContext around to every view and it wouldn't be asynchronously like when I implement delegate methods that just gets called when a result is ready.

I hope this makes sense and that someone can either confirm that there is no other reasonable way to do it or help point me in a direction for wrapping this up in a good way.

Thanks:)

+5  A: 

Core Data is not nearly as complicated as you describe.

Generally, an iPhone app has a "main" managed object context, which is generally owned by the app delegate. So long as you can get the app delegate (hint: [[UIApplication sharedApplication] delegate]) you have access to the managed object context. I like to define a static global variable to hold a reference to my app delegate to make life easier.

There's generally a one-to-one correspondence between NSFetchedResultsController instances and UITableView instances. Aside from populating table views, it's exceedingly rare that you would need an NSFetchedResultsController. If you have a number of similar views (e.g. a tab bar that lets you view the same data in different ways a la the iPod app), it would behoove you to create a single base class that configures the NSFetchedResultsController and derive your specific view controllers from that.

Now, when you create view controllers to edit an object, it's generally a good idea to do that in a separate managed object context. If the user cancels, you just discard the context and the changes go away. Again, you don't really need an NSFetchedResultsController for this because these views are only concerned with a single object.

When you're done editing, you save: the managed object context. The objects that manage your other managed object contexts should implement the NSFetchedResultsControllerDelegate methods to keep the table view in sync. Again, this can be implemented in a base class so you can generalize this functionality for related view controllers.

Alex
A: 

Do you absolutely have to use a CoreData model, or would something using a NSCoder (NSArchiver, NSKeyedArchiver, etc) work? I've found that CoreData is overkill for most applications.

Also, could you clarify why you can't take an approach using singletons? I've used singleton factories in a number of applications without issues. It's fairly easy to define class-level methods that operate on a shared (singleton) instance.

D Carney
Alex you are right:) I have blown the resultsController a bit out of proportions because I was having 3 tableviews chained together, in all other views a resultsController would be pointless.So using the managedObjectContext object that is instantiated in my app delegate through [UIApplication sharedApplication] delegate]) is the way to go, that makes sense, a bit like a singleton.D Carney, I will be maintaining a rather large model and it will sync up against a web service exposing a model just like it (a local and global version of my data) so I feel like core data is the stuff.
RickiG
Im not sure the singleton approach wouldn't work, but I would probably get timing issues if the data wasn't ready or couldn't be saved.Thank you both, I feel I got a bit clearer view on ManagedObjectContexts and the alternatives to Core Data:)
RickiG
Methods that operate on a singleton instance are by instance methods, not class methods.
Jonathan Sterling
Now you're just debating semantics. If you're creating a singleton in Obj-C, it's convenient to create class-level methods that, in turn, access a global (singleton) instance for that class.
D Carney