CoreData issue with appDelegate
I'm relatively new to Objective C. So far everything has been going really well until I hit CoreData. I just can't get it to work! After spending many hours on something that seems to be pretty straightforward, I'm at my wits' end.
PLEASE help me figure out what I have done wrong:
- I created a new Windows-Based app and checked 'use Core Data for storage'
- In the xcdatamodel, I created an entity named 'RecipeData' with only one attribute 'recipeName' it is a string
- in the app delegate, I load an XML file and parse it. When I parse the recipe name, I use the following:
recipeData *dataName = (recipeData *) [NSEntityDescription insertNewObjectForEntityForName:@"RecipeData" inManagedObjectContext:managedObjectContext]
;
I get the following error:
terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'RecipeData'
Which leads me to the big 3 questions:
- is there anything really obvious that I am doing wrong?
since I checked 'use Core Data for storage,' it seems the following code is injected automatically into the app delegate .h:
@private NSManagedObjectContext *managedObjectContext_;
NSManagedObjectModel *managedObjectModel_; NSPersistentStoreCoordinator *persistentStoreCoordinator_;
@pr开发者_运维百科operty (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
Does this interfere with the code I am using?
- I tried creating a new NSManagedObjectContext called *myManagedObjectContext but that did not work.
One other tidbit, when I add the following right above my code:
if (managedObjectContext == nil) {
NSLog(@"NO CONTEXT");
}The console prints "NO CONTEXT"
I really appreciate any help. Thanks.
Where has managedObjectContext
come from? Is it a typo for managedObjectContext_
? The project templates create the latter, not the former. Using the code above with the code provided by the standard project templates should produce a syntax error. I'm guessing you've renamed some things?
You seem to be using managedObjectContext
as an ivar. It is a property. Inside the class, there is a private managedObjectContext_
ivar which holds the reference to the object context. You shouldn't access this. You should be accessing the managedObjectContext
property. When this property is first accessed, its getter method will create the context for you. Since you aren't accessing the property, the getter method isn't called and the context never gets created.
Where you have code like this:
recipeData *dataName = (recipeData *) [NSEntityDescription insertNewObjectForEntityForName:@"RecipeData" inManagedObjectContext:managedObjectContext];
...you should be using code like this:
recipeData *dataName = (recipeData *) [NSEntityDescription insertNewObjectForEntityForName:@"RecipeData" inManagedObjectContext:self.managedObjectContext];
Note the self.
bit. This means that you are accessing a property on the self
object, not accessing an ivar from the object the method is being called on.
Note that reading a property is the same as calling the getter method, so the above can also be written as:
recipeData *dataName = (recipeData *) [NSEntityDescription insertNewObjectForEntityForName:@"RecipeData" inManagedObjectContext:[self managedObjectContext]];
精彩评论