Re: Object scope - or where to put objects that should always be accessible
Re: Object scope - or where to put objects that should always be accessible
- Subject: Re: Object scope - or where to put objects that should always be accessible
- From: "R. Scott Thompson" <email@hidden>
- Date: Tue, 26 Oct 2004 16:26:42 -0500
On Oct 26, 2004, at 4:16 PM, Ole Voss wrote:
I'm getting into Cocoa development and have a bit of a problem with the
object-structure. I see that objects are being instantiated by the nib
(for
instance) but where do I put objects that I want to be globally
accessible.
For example: I would like my program to establish a database connection
during its startup and keep this database object available throughout
the
applications life. When the application terminates, the object should
close
the database connection. Where do I instantiate this object? Should it
be in
the 'main' Routine or is there a special place where I should put it.
Also, what about the other Objects that are brought to life by the nib
loader for instance. Are these objects accessible to all other objects?
It there any good place to read about object scope and all these
things?
Please point me in the right direction. I don't want to bother anybody
with
simple stuff ;-)
A good example of one such object is the shared application object.
You can get at it using:
[NSApplication sharedApplication];
(or more often just using the NSApp variable).
What I've done, for better or worse, is followed that convention. I
have a class that sounds similar to the one you mention above called
the MacDBManager.
Inside the MacDBManager class I have a class method:
+ (MacDBManager *) sharedDatabaseManager;
the implementation of sharedDatabaseManager is something like:
+ (MacDBManager *) sharedDatabaseManager
{
static MacDBManager *gSharedDBMgr = NULL;
if(!gSharedDBMgr) {
gSharedDBMgr = [[MacDBManager alloc] init];
}
check(gSharedDBMgr);
return gSharedDBMgr;
}
Then anybody that wants the shared db manager simply calls the routines
above.
Scott
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden