Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[NSFileManager copyItemAtPath:toPath:error:]: source path is nil

I am trying to add a sqlite DB into an application using this: CoreData: Preload Data in Your iOS App

Following the tutorial i hit a problem:

[NSFileManager copyItemAtPath:toPath:error:]: source path is nil 

I use the following code:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (__persistentStoreCoordinator != nil) {
    return __persistentStoreCoordinator;
}

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Peter_R0_1.sqlite"];

if(![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]]) {
    NSString *sqlitePath = [[NSBundle mainBundle] pathForResource:@"Peter_R0_1" ofType:@"sqlite" inDirectory:nil];
    NSError *anyError = nil;
    BOOL success = [[NSFileManager defaultManager] copyItemAtPath:sqlitePath toPath:[storeURL path] error:&anyError];
}


NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
{

    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}    

return __persistentStoreCoordinator;

}

I would very much appreciate some help on this problem

like image 625
PeterK Avatar asked Dec 03 '22 03:12

PeterK


1 Answers

Well, that error means the NSString *sqlitePath is == nil so 99% chance it's one or more of the following:

  1. The file is not added to the correct Target in the project
    • Verify via "Target Membership" under the File Inspector view for the Peter_R0_1.sqlite file)
  2. And/Or: You're copying the file to a specific sub-directory instead of the default.
    • Verify the file is listed under "Copy Bundle Resources" under your Target's "Build Phases" and NOT under "Copy Files" with a custom destination or sub-path.
  3. And/Or: You've got a typo in the file name.
like image 95
MechEthan Avatar answered Mar 16 '23 11:03

MechEthan