/**
Returns the path to the application's Documents directory.
*/
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
- (NSManagedObjectContext *) managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return managedObjectContext;
}
/**
Returns the managed object model for the application.
If the model doesn't already exist, it is created by merging all of the models found in the application bundle.
*/
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
return managedObjectModel;
}
/**
Returns the persistent store coordinator for the application.
If the coordinator doesn't already exist, it is created and the application's store added to it.
*/
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"A1HandyFahrschein.sqlite"]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
Typical reasons for an error here include:
* The persistent store is not accessible
* The schema for the persistent store is incompatible with current managed object model
Check the error message to determine what the actual problem was.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator;
}
When I am trying to save from a ViewController (I am using the same code as above in my ViewController Class" the Data will not be saved.
When I am trying to save from a ViewController (I am using the same code as above in my ViewController Class" the Data will not be saved.
Well, you don't actually have any code shown in your post to save the data. All you show is inserting a new object, but never saving the changes.
Also, it's not clear what you mean when you say you are using the same code as above in your view controller. Can you post the code from your view controller?
__________________ Recall It!Tag your notes. Tag your photos. Tag your thoughts. Tag your life.
CoreData: Save possible in AppDelegate and not in ViewController
Hi StefanL and dljeffery,
I'm working on my first App and am attempting to implement Core Data in a UIViewcontroller while following guides and sample code that use a UITableviewcontroller (Apple's books and recipes sample Core Data Apps), and I am experiencing PERHAPS(?) a similar problem as you have:
I run similar "boiler plate" code as yours in the AppDelegate and successfully read, add, and change data in the delegate. However in the UIViewcontroller code, before I get a chance to read or change any data, my App aborts in viewDidLoad, where I'm running this code:
// My debugging attempt here produces "null" values (!!!) on the console for each of these logs:
NSLog(@"Context: %@",context);
NSLog(@"PS Coord : %@",context.persistentStoreCoordinator);
NSLog(@"MOM : %@", context.persistentStoreCoordinator.managedObjectMo del);
// And then there here my program aborts with the error message just below:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Activations" inManagedObjectContext:[self managedObjectContext]]; // Error message on the console:
"+entityForName: could not locate an NSManagedObjectModel for entity name"
I am wondering that I don't know what UIViewcontroller needs to do to make use what was set up in the App Delegate?
If you have found a solution I would be very interested to learn what you did!
Thank you,
Bill
Sounds like you just never set a value for managedObjectContext on your controller instance after creating the instance and before pushing/displaying it.
__________________ Recall It!Tag your notes. Tag your photos. Tag your thoughts. Tag your life.
Sounds like you just never set a value for managedObjectContext on your controller instance after creating the instance and before pushing/displaying it.
Thank you, I realize now I could use the App Delegate in the instance and added 2 lines just before the fetch call, which solved the problem, thank you again: