Reputation: 668
- (void)doneButtonPressed
{
//判断已存在这个title的proj的话不创建
//存储CD数据
Project *newProject = [NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:self.managedObjectContext];
//!!!!crash here!!!!
newProject.title = self.titleTextField.text;
newProject.subtitle = self.subtitleTextField.text;
newProject.progress = [NSNumber numberWithFloat:1.0f];
newProject.pid = @"001";
NSError* savingError = nil;
if (newProject != nil){
if ([self.managedObjectContext save:&savingError]){
NSLog(@"Saved!");
}
else{
NSLog(@"Fail to save the proj");
}
}
else{
NSLog(@"Fail to create new proj");
}
}
I simply add these code into my CertainViewController.m
and found that I could not save things into the core data.
Here below shows the reason:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Project''
Do I miss some pre-setting in the CertainViewController
?
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) NSEntityDescription *entityDescription;
I do include these two property into the .h and synthesize them in the .m
.
Will really appreciate your answer;)
Upvotes: 0
Views: 64
Reputation: 6011
The answer is simple. you have not set your context properly.
If i guess correctly (and I do cause I tested it), you have only alloc
init
your context like so:
self.managedObjectContext = [[NSManagedObjectContext] alloc] init];
A context defined like this is orphaned as it does not have a NSPersistentStoreCoordinator
or parentContext
to get to the persistent store from.
You should either pass the context from the previous view controller, or set it up yourself:
//Example:
AppDelegate* delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
self.managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:<#(NSManagedObjectContextConcurrencyType)#>];
[self.managedObjectContext setPersistentStoreCoordinator:delegate.persistentStoreCoordinator];
Upvotes: 1
Reputation:
Check the following steps:
Try delete app from simulator or Device after run
Check the below tutorials, http://www.raywenderlich.com/934/core-data-on-ios-5-tutorial-getting-started http://mobile.tutsplus.com/tutorials/iphone/iphone-core-data/
Upvotes: 0