views:

148

answers:

3

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
A: 

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.

Shaggy Frog
I thought I already created a datamodel. I added an entity, and attributes to the datamodel, as well as a class specific for that entity as described in a core data tutorial.
kevin Mendoza
Check the application bundle in your build directory. Is there a `.mom` file present? (Right-click on Target, Reveal in Finder, right-click on .app, Show Package Contents) If not, and you're sure you have an `xcdatamodel`, then make sure the `xcdatamodel` file is listed under the "Compile Sources" section of your Target.
Shaggy Frog
Right clicking on the target on xcode does not bring up a reveal in finder option. however searching through my computer's files I could not find a .mom file.
kevin Mendoza
Right click on *the* target, and you will get the menu I described.
Shaggy Frog
as in the blue thing that bears the title of my app? opening that in finder, and opening package contents shows only three files of extentions .mode1v3 , .pbxuser , and .pbxproj
kevin Mendoza
Shaggy Frog
It does not give me the option to reveal it in finder.
kevin Mendoza
Then you are not clicking on the right thing.
Shaggy Frog
+1  A: 

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.

Marcus S. Zarra
I am getting it from the USGS website. What I am doing is loading it directly using sql statements in the application, creating objects out of that data, and then storing them in core data.
kevin Mendoza
So in effect I am not using coredata to read the populated database, but I am using coredata to store it in another database for future use in my program.
kevin Mendoza
A: 

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.

TechZen
ok that makes more sense. I changed the strings to my App name and mom, and now when the program crashes it gives me this error:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'Which leads me to believe that it cannot find the mom file. Which is weird. Heck I cant seem to find a mom file anywhere on my mac, so maybe core-data is not making such a file?
kevin Mendoza
In your project in code, you will have file with an extension of `xcdatamodel` (it's the one with you entity graph.) The `mom` file in the build will have the same name as that file. You can also use `modelByMergingModels:` which will automatically locate all the model files for you.
TechZen
I selected yours as the answer because it was the closest to what I found worked. it had to be pathForResource:@"AppName" ofType:@"momd"
kevin Mendoza
The `momd` is a versioned model file.
TechZen