简体   繁体   中英

copying from main bundle creates a file size zero kb

As part of my app start-up i copy bundle files to my documents directory.

This works fine for three out of four of my files but the fourth one create a Zero KB file.

running on iOS 5.0 sim. I have cleaned the build several times and checked the file name capitalization vis correct.

the file appears in the directory but is zero kb and should be 24K

any help appreciated.

-(BOOL) CheckDBs: (NSString *)dbname 
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];   
    NSString *dbPath = [documentsDir stringByAppendingPathComponent:dbname];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL success = [fileManager fileExistsAtPath: dbPath];
    NSLog(@"AppDelegate CheckDatabase: %@ = %i", dbPath, success);

    if (success) {
        //NSLog(@"return YES");
        return YES;
    }
    else {
        return NO;  
    }   
}  // Complete - checks if files exist in the User Documents directory

-(void) copyDBs: (NSString *) dbname 
{
    //Using NSFileManager we can perform many file system operations.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *dbPath = [documentsDir stringByAppendingPathComponent:dbname];        
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbname];
    BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

    if (success) {

        // Version 4.0 code
        //NSDictionary *attribs = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
        //success = [fileManager setAttributes:attribs ofItemAtPath:dbPath error:&error];
        NSLog(@"AppDelegate copyDatase: %@ = %d", dbPath, success); 
    }

    //NSLog(@"AppDelegate copyDatase: %@ = %d", dbPath, success);   
    if (!success) {

        NSLog(@"Failed to copy database: '%@'", [error localizedDescription]);
        //  NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }   
}

Have you also checked the original file size?

Try resetting your simulator. From the NSFileManager documentation:

If a file with the same name already exists at dstPath, this method aborts the copy attempt and returns an appropriate error.

Make sure the destination is empty and try again. Also, check the error object.

If all that checks out there has got to be an error in spelling the file name . Check if the exact file exists in bundle, NSLog wherever you use a file name or path, etc. You should find the error. Also check the appropriate folder in the Finder.

Instead of using

[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbname]

try

[[NSBundle mainBundle] pathForResource:shortName ofType:@"db"]

Ok I figured out what is causing the problem.

as i run the app the appdidfinishlaunching method is not complete before one of the view controllers is loading. That view controller attempts to access one of the files being copied over from the bundle.

I'm guessing that sqlite creates the file when you attempt to access the database, it creates it with with a zero bytes length.

So when my appdidfinish launching method checks for the existance of the file it exists due to the sql call.

This is usually only going to be a problem prior to the first run of the app as after that the database will exist.

problem now is how do i get the appdidfinish launching to complete prior to the rest being allow to start as the view controller in question is part of the mainwindow.xib

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM