objective c - Singleton not initializing correctly -


i have following code calling using statement: sqlitedb *db = [[[sqlitedb alloc] init] autorelease];

the problem "sharedsqlitedb" not being called, rather "allocwithzone" is, , therefore "checkifdatabaseexists" not being called, database created.

i don't understand why... (i.e. doing wrong?)

#import "sqlitedb.h"  static sqlitedb *sharedsqlitedb = nil;  //  makes singleton class  @implementation sqlitedb  @synthesize searchpaths, documentpath, databasepath, cdatabasepath;  #pragma mark singleton methods  + (sqlitedb *) sharedsqlitedb  {      if(!sharedsqlitedb)  {         sharedsqlitedb = [[sqlitedb alloc] init];         [sharedsqlitedb checkifdatabaseexists];  //  check see if d/b exists     }     return sharedsqlitedb; }     +(id)allocwithzone:(nszone *)zone  {  //  makes sure instance not allocated     if(!sharedsqlitedb)  {         sharedsqlitedb = [super allocwithzone:zone];         return  sharedsqlitedb;     }     else {         return nil;     } }     -(id)copywithzone:(nszone *)zone  {     return self; }  -(void) release  {     //  no-op } 

it isn't executing + (sqlitedb *) sharedsqlitedb method because you're not calling method anywhere.

as you've seen, when call [[sqlitedb alloc] init], allocwithzone method called.

change call sqlitedb *db = [sqlitedb sharedsqlitedb], call checkifdatabaseexists method in case. however, if [[sqlitedb alloc] init] called somewhere else, checkifdatabaseexists method call still skipped.

maybe consider moving checkifdatabaseexists method init method called both singleton method , allocwithzone.


Comments