Pangolin
Pangolin

Reputation: 7444

Copying file from bundle to documents not working (Cocoa error 512)

I am trying to simply copy my sqlite3 database to the documents directory. I get a Cocoa Error 512 and what I figured about that is, that it's not a valid directory (or something like that.

The database file is in my Resources folder in XCode. (The name of file is correct)

Here is the code I am trying to use:

-(void) checkAndCreateDatabase
{
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];

    //databasePath + databaseName is declared in the header
    databaseName = @"WaypointDatabase.sql";
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

    success = [fileManager fileExistsAtPath:databasePath];
if(success)
    {
        NSLog(@"Database exists");
            return;
    }
    else
        NSLog(@"Database does not exists");

    NSString *databasePathFromApp = [[NSBundle mainBundle] pathForResource:@"WaypointDatabase" ofType:@"sql"];
    if(databasePathFromApp == nil)
    {
        NSLog(@"ERROR: IT IS NIL");
    }
    NSError* error = nil;

    NSLog(@"Path in bundle:\n%@\n\n", databasePathFromApp);
    NSLog(@"Path to copy to:\n%@\n\n", databasePath);

    [fileManager copyItemAtPath:databasePathFromApp
                     toPath:databasePath error:&error];
    [fileManager release];
    if (error)
    {
        NSLog(@"%@\n\n", error);
        NSLog(@"%@", [error userInfo]);
    }
}

And the output to console that I get is:

2011-10-30 10:36:13.242 xxxx[6726:707] Database does not exists
2011-10-30 10:36:13.249 xxxx[6726:707] Path in bundle:
/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/xxxx.app/WaypointDatabase.sql

2011-10-30 10:36:13.252 xxxx[6726:707] Path to copy to:
/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/Documents/WaypointDatabase.sql

2011-10-30 10:36:13.268 xxxx[6726:707] Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0xe8b7cd0 {NSUserStringVariant=( Copy ), NSFilePath=/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/xxxx.app/WaypointDatabase.sql, NSDestinationFilePath=/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/Documents/WaypointDatabase.sql, NSUnderlyingError=0xe8b7ee0 "The operation couldn’t be completed. Not a directory"}

2011-10-30 10:36:13.272 xxxx[6726:707] { NSDestinationFilePath = "/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/Documents/WaypointDatabase.sql"; NSFilePath = "/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/xxxx.app/WaypointDatabase.sql"; NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=20 \"The operation couldn\U2019t be completed. Not a directory\""; NSUserStringVariant = ( Copy ); }

I figure this must be to do with getting the directory paths or something, but have been stuck on this for a few days now.

Take note, this works perfectly & without any errors on the simulator.

Where could I have gone wrong?

[UPDATE]

Could this be because the Documents-folder directory does not exist? How would I go about to create/check it?

[UPDATE 2]

I have done some other tests using

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *err = nil;
NSArray *dirArr = [fileManager contentsOfDirectoryAtPath:documentsDir error:&err];
NSLog(@"~~Contents:\n%@",dirArr);
NSLog(@"~~Error: \n%@",err);

And surprisingly it gave Cocoa Error 256 and also said "Not a directory". It is almost as if the Documents directory does not exist. But it does, according toNSSearchPathForDirectoriesInDomains`

Here is the output I got

2011-10-30 10:59:16.934 xxxx[6774:707] ~~Contents:
(null)
2011-10-30 10:59:16.936 xxxx[6774:707] ~~Error:
Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x10052580 {NSUserStringVariant=( Folder ), NSFilePath=/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/Documents, NSUnderlyingError=0x10054a50 "The operation couldn’t be completed. Not a directory"}

Upvotes: 2

Views: 5592

Answers (2)

Pangolin
Pangolin

Reputation: 7444

The problem got solved by setting a non standard Bundle ID in die info.plist

I used the Bundle ID from iTunes Connect for this specific app. Now everything works perfectly.

Upvotes: 2

Paul.s
Paul.s

Reputation: 38728

Try changing

[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

for

[[NSBundle mainBundle] pathForResource:@"WaypointDatabase" ofType:@"sql"];

and check that this does not return nil to ensure that the is not what's going wrong apart from that it looks fine to me on first skim.

Upvotes: 0

Related Questions