Reputation: 555
Currently I have created a Objective-C Class( NSObject) to handle any special functions that I need, that involve interaction with my CoreData model. These functions that interact with my CoreData are located in dbInterface.h and dbInterface.m files. In the dbInterface class I have added a conform <NSFetchedResultsControllerDelegate>
.
@interface dbInterface : NSObject<NSFetchedResultsControllerDelegate>{
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;
}
And in the app delegate I have created an instance of this class, and connected the delegate managed context with the dbInterface object managed context:
appDelegate.h:
@property (nonatomic, retain) IBOutlet dbInterface *dbConnector;
appDelegate.m:
@synthesize dbConnector;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
dbConnector.managedObjectContext=self.managedObjectContext;
return YES;
}
Question : I have 6 view controllers which must interact with this dbInterface object, to gather data for the view controller itself. So in this setup the dbInterface is acting as a middleman for the viewController and CoreData model. Is this the right way to perform these specific operations I have defined in the middleman, the dbInterface class?
If it is NOT the right way, then how should I go about interacting with the CoreData model for the view controllers sake?
If it is correct then how do I get access to the object that I declared in the app delegate dbConnector
? Obviously I do not want to create 6 copies of dbInterface class. This has the problem of using too much memory and then having to also write 6 copies of this line:
dbConnector.managedObjectContext=self.managedObjectContext;
I have heard the term singleton bandied about, is this the way to go?
Upvotes: 0
Views: 94
Reputation: 3821
You should be able to reference the instance of your class from your app delegate. Here is the code that I use to reference my app delegate from another class. You'll have to change your class prefix to whatever you use.
#import "NXAppDelegate.h"
NXAppDelegate *appDelegate = (NXAppDelegate *)[[UIApplication sharedApplication] delegate];
Since you're using dbConnector as a property, you can reference it from the app delegate (example below):
viewController.managedObjectContext = appDelegate.dbConnector.managedObjectContext;
Using a singleton is frowned upon a little is iOS development. It has a tendency to be over-used or used without really needing to. If you have evaluated your conditions and cannot use delegates, protocols or subclassing to accomplish your needs, I would suggest using a singleton.
It looks like Apple has updated their documentation to help explain the proper use of singletons if you would like to read up on the topic.
Also, there are plenty of articles online to talk about the pros/cons of singletons.
http://jason.agostoni.net/2012/01/22/ios-best-practices-singletons/
Upvotes: 1