I have my main app delegate
I have a few UIViewController derived instances driven by a Storyboard
Say I’d like to provide a centralized persistence layer for my application – perhaps Core Data of SQLite. Where would I put those objects? I’m missing some centrally accessible “Application” class you can access from all the UIViewController instances.
Is there a pattern to follow here?
What you’re describing is your model layer. There are two main ways to manage the model:
The “main model object” in both cases is generally some kind of object manager. It could be a document, or it could be a
PersonManagerif you have a bunch ofPersonobjects. This object will vend model objects from your persistence store (generally Core Data).The advantage of a Singleton here is that it’s a little easier to implement and you don’t have to pass around the manager. The advantage of a non-Singleton is that it’s easier to have more than one (for a document-based system), and it’s easier to test and reason about non-singletons than singletons. That said, probably 80% of my projects use a singleton model manager.
As a side note, that you appear to already understand: never store the model in the application delegate, and never use the application delegate as a “rendezvous point” to get to the model. That is, never have a
sharedModelmethod on the application delegate. If you find yourself calling[[UIApplication sharedApplication] delegate]anywhere in your code, you’re almost always doing something wrong. Hanging data on the application delegate makes code reuse extremely difficult.