Here's what I've done: Created the sqlite file with a populated version of the app. Copied that file into my app bundle, making sure that the target is set and that it is present in "Copy Bundle Resources"
I then try to populate Core Data like this:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"TestModel.sqlite"];
if( ![[NSFileManager defaultManager]
fileExistsAtPath:[storeURL path]] ) {
// If there’s no Data Store present (which is the case when the app first launches), identify the sqlite file we added in the Bundle Resources, copy it into the Documents directory, and make it the Data Store.
NSString *sqlitePath = [[NSBundle mainBundle]
pathForResource:@"TestModel" ofType:@"sqlite"
inDirectory:nil];
NSError *anyError = nil;
BOOL success = [[NSFileManager defaultManager]
copyItemAtPath:sqlitePath toPath:[storeURL path] error:&anyError];
if(success){
NSLog(@"sucess, loading into store");
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();
}else{
NSLog(@"error with sqlite file");
}
}
}
return _persistentStoreCoordinator;
}
But I get this error:
NSFileManager copyItemAtPath:toPath:error:]: source path is nil
What am I doing wrong? Is it saying that it can't find the sqlite file in the bundle?
I think this happened because when I imported the file from Finder into Xcode, it was named "TestModel" instead of "TestModel.sqlite".