Question

my program needs to take a prepopulated sql database and then save the records to the app's database. Unfortunately for some reason the application quits in this method in the application delegate:

#pragma mark -
#pragma mark Core Data stack
- (NSManagedObjectModel *)managedObjectModel {
    if (managedObjectModel_ != nil) {
        return managedObjectModel_;
}
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"iProspectLite" ofType:@"sqlite"];
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
return managedObjectModel_;
}

it seems as if the application cannot find a valid managedObjectModel_, or it doesn't exist, or is not creating one. How do i solve this?

one of the error messages I get on the console is this: Terminating app due to uncaught exception reason: [NSKeyedUnarchiver initForReadingWithData:]

part of this I have narrowed down to the NSManagedObject, in that there doesn't seem to be one created or found.

Other information that may be helpful: I added an entity and defined attributes as described in many other core-data tutorials the following is the class that defines my entity:

#import "Mine.h"


@implementation Mine 

@dynamic primarykey;
@dynamic name;
@dynamic firstCommodity;
@dynamic longitude;
@dynamic county;
@dynamic secondCommodity;
@dynamic latitude;
@dynamic thirdCommodity;

@end
Was it helpful?

Solution

You problem is that your trying to load your model file from the path to an SQL persistent store inside the app bundle. Stores and model files are two separate types of files with two separate functions. You can't initialize a NSManagedObjectModel instance from an SQL file and vice versa because the two file types hold completely different information.

You need to change the pathForResource: to look for the right file. The file type is mom and the default name is the app name so you should use something like:

NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"AppName" ofType:@"mom"];

(If you named you model something else, use that name.)

All this has nothing to do with importing an existing SQL file. However, you will need to fix this before importing the SQL as other's have suggested.

OTHER TIPS

Where are you getting the pre-populated SQLite database? If it is not created with Core Data then it is not going to work. Core Data can only read SQLite files that are created with Core Data.

From Apple's Core Data Development Guide:

Compiling a Data Model

A data model is a deployment resource. In addition to details of the entities and properties in the model, a model you create in Xcode contains information about the diagram—its layout, colors of elements, and so on. This latter information is not needed at runtime. The model file is compiled to remove the extraneous information and make runtime loading of the resource as efficient as possible. The xcdatamodel "source" file is compiled into a mom deployment file using the model compiler, momc.

In other words, Managed Object Models are "compiled" from Xcode Data Model "source" by Xcode during the build process, and that .mom file is placed inside the application bundle. So you may need to create a Data Model file manually.

To work around this, I use a Core Data-savvy command line tool to pre-populate the SQLite database files I use with my iPhone projects.

Edit:

If Mr. Zarra is correct (and he usually is), then you should write a command line tool in the manner I described, to open the existing database and then pre-populate a new SQLite database file with Core Data. It's a bit of a pain, but it will work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top