Singleton problem when called from thread
Singleton problem when called from thread
- Subject: Singleton problem when called from thread
- From: Ken Tozier <email@hidden>
- Date: Fri, 28 Apr 2006 04:37:32 -0400
Hello
I wrote a singleton class to manage connecting to and querying a
database and am having problems calling it from inside a thread. When
I send it queries in the main thread, it works fine but when called
from a forked thread, critical parts of it's init code never get called.
I tried initializing it before doing anything else in my app but
still no luck. I put NSLogs before practiacally every line of code
but calls to it's "performQuery:" method just seem to vanish into the
ether. None of the NSLogs ever print.
Here's the singleton. Could someone give a few pointers on how to
tweak it so it can be called from objects in other threads?
Thanks for any help
Ken
@implementation CNCDBProxy
static KDatabaseConnector *gConnector = nil;
static NSMutableDictionary *gQueries = nil;
+ (void) initialize
{
if (gConnector == nil)
{
NSDictionary *params = [CNCDBProxy loginInfo];
// initialize the singleton
gConnector = [[KDatabaseConnector databaseConnector] retain];
// connect to the database
[gConnector connectToDatabase: params];
// get the saved queries
NSArray *temp = [gConnector performQuery: @"select name,
query_text from saved_query"];
// reformat into a dictionary for quicker access
NSEnumerator *enumerator = [temp objectEnumerator];
id anObj;
gQueries = [[NSMutableDictionary alloc] init];
while ((anObj = [enumerator nextObject]) != nil)
{
NSArray *props = [anObj allValues];
[gQueries setObject: [props objectAtIndex: 1] forKey: [props
objectAtIndex: 0]];
}
}
}
+ (id) defaultDBProxy
{
return gConnector;
}
+ (NSArray *) performSavedQuery:(NSString *) inQueryName
{
return [gConnector performQuery: [gQueries objectForKey: inQueryName]];
}
+ (NSArray *) performQuery:(NSString *) inQuery;
{
return [gConnector performQuery: inQuery];
}
+ (NSDictionary *) loginInfo
{
NSString *x = [NSBundle pathForResource: @"db_login" ofType:nil
inDirectory: [[NSBundle mainBundle] bundlePath] ] ;
return [NSDictionary dictionaryWithContentsOfFile: x];
}
@end
_______________________________________________
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