Well it's nothing terribly magical, and I'll assume that anyone reading this can parse out the header vs module code and that stubs of course have code in them, but maybe this will help the idea:
In app delegate, I have a property to access the data manager class instance:
Code:
// header
@class DataManager;
@interface myAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
DataManager *manager;
}
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) UINavigationController *navigationController;
@property (nonatomic, retain) DataManager *manager;
@end
// module
@synthesize manager;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// initialize our data manager
DataManager *myManager = [[DataManager alloc] init];
self.manager = myManager;
[myManager release];
// Configure and show the window
navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
- (void)dealloc {
[manager release];
[navigationController release];
[window release];
[super dealloc];
}
Header for the DataManager class
Code:
#import <Foundation/Foundation.h>
#define kDatabaseName @"mydatabase.sqlite"
@class FMDatabase;
@class Foo;
@class Bar;
@interface DataManager : NSObject {
FMDatabase *database;
NSAutoreleasePool *pool;
NSMutableArray *foos;
NSMutableArray *bars;
}
@property (nonatomic, retain) NSMutableArray *foos;
@property (nonatomic, retain) NSMutableArray *bars;
- (void) getFoos;
- (NSInteger) addFoo:(Foo *)myfoo;
- (void) updateFoo:(Foo *)myfoo;
- (void) deleteFoo:(Foo *)myfoo;
- (void) getBarsForFoo:(int)fooid;
- (NSInteger) addBar:(Bar *)mybar;
// etc...
@end
Like I said, nothing super magical here, but it at least consolidates my data access code and the data points that need to be shared in the application without cluttering the delegate. To access data from somewhere I can get at one of the data points using something like the following:
Code:
MyAppDelegate *delegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate;
DataManager *manager = delegate.manager;
Foo *iPityTha = (Foo *)[manager.foos getObjectAtIndex:2];
This has seemed to work decently well for me, but I'm open to any suggestions for improvement. =)