App quitting on [fm fileExistsAtPath] call?


#1

I’m getting some weird behavior, where the app is crashing on the [fm fileExistsAtPath] call. This happens in both the simulator and on the device (iPad sdk 3.2)

  1. I added a / to the beginning of the countries.db since I noticed when I printed out the full path it was ending in Documentscountries.db instead of Documents/countries.db. The program was still crashing before I added the /)

  2. I extracted out the exists BOOL just so I could wrap the fileExistsAtPath call in a try/catch, which for whatever reason, the app is still crashing.

  3. Also, I’ve verified that countries.db is in my Resources group, I’ve verified that it gets copied over under Targets/Nayshunz/Copy Bundle Resources, and I did the terminal creation of the db from the sql file

  4. The last statement that shows up in the debugger is
    2010-07-23 07:44:54.613 Nayshunz[13567:207] running fileExistsAtPath method now

I’ll get the exc_bad_access message when running on the device just after this debug line

  1. Sometimes this runs, like maybe once out of every 10 attempts. I’ve also noticed if I comment out the fileExistsAtPath call so that ‘exists’ never gets set and I always copy the db file across, I’ll get a “file already exists” message in the debugger, but the app will still run as expected.

The init method

  • (id) init {
    [super init];

    //create the root of the tree
    continents = [[NSMutableArray alloc] init];

    //where do the docs go?
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];

    //Name of the db file
    NSString *fullPath = [path stringByAppendingFormat:@"/countries.db"];

    //get a file manager for file operations
    NSFileManager *fm = [NSFileManager defaultManager];

    NSLog(@“seeing if countries.db exists at %@”, fullPath);
    //does the file already exist
    BOOL exists = NO;
    @try{
    NSLog(@“running fileExistsAtPath method now”);
    exists = [fm fileExistsAtPath];
    } @catch (NSException *e )
    {
    NSLog(@“caught exception while trying to see if file exists %@, " , e);
    }
    NSLog(@“dump? %@”, exists);
    if ( exists ){
    NSLog(@”%@ exists- just opening", fullPath );
    } else {
    NSLog(@"%@ doesn’t exist, starting db in the application wrapper?", fullPath);
    //where is the starting database in the application wrapper
    NSString *pathForStartingDB = [[NSBundle mainBundle] pathForResource:@“countries” ofType:@“db”];
    //copy to docs dir
    NSError *error;
    BOOL success = [fm copyItemAtPath:pathForStartingDB toPath:fullPath error:&error];
    if (!success ){
    NSLog(@“db copy failed, %@”, error);
    }
    }

    //open the db file
    const char *cFullPath = [fullPath cStringUsingEncoding:NSUTF8StringEncoding];
    if ( sqlite3_open(cFullPath, &database) != SQLITE_OK)
    {
    NSLog(@“unable to open db at %@”, fullPath );
    }
    return self;
    }


#2

Just realized I needed to update this call, which makes it so I don’t need to add “/” to countries.db

NSString *fullPath = [path stringByAppendingPathComponent:@“countries.db”];

The program is still crashing at the fileExistsAtPath call