@implementation CoreDataStack
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
-(void)reportOnAllPeopleToLog {
NSLog(@"------- logging out results --------------");
//Get all the projects in the data store
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person"
inManagedObjectContext:[self managedObjectContext]];
request.entity = entity;
NSArray *listOfPeople = [[self managedObjectContext] executeFetchRequest:request error:nil];
//List out contents of each project
if([listOfPeople count] == 0)
NSLog(@"There are no peple in the data store yet");
else {
NSLog(@"HERE ARE THE People IN THE DATA STORE");
[listOfPeople enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%lu ----------------", idx);
NSLog(@"firstName = %@", [obj firstName]);
NSLog(@"lastName = %@", [obj lastName]);
//[[self managedObjectContext] deleteObject:obj];
//[[self managedObjectContext] save:nil];
}];
}
}
+(instancetype)defaultStack {
//all instantiations of THCoreDataStack, will access this method
//we use this one and only statick variable
static CoreDataStack * defaultStack;
//NSLog(@"address of defaultStack - %p", defaultStack);
if(!defaultStack) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//here, code gets executed once
//hence we alloc our one and only defaultStack variable once
defaultStack = [[self alloc] init];
//NSLog(@"ALLOC defaultStack on GCD - %p", defaultStack);
});
}
return defaultStack;
}
- (NSURL *)applicationDocumentsDirectory {
// The directory the application uses to store the Core Data store file. This code uses a directory named "rickytsao.DataCoreTut" in the application's documents directory.
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
- (NSManagedObjectModel *)managedObjectModel {
NSLog(@"(NSManagedObjectModel *)managedObjectModel - database schema, uses .momd file to create object model");
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"CoreData2" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
NSLog(@"(NSPersistentStoreCoordinator *) persistentStoreCoordinator - database connection, coordinates between our object model and .sqlite file. Our connection to the .sqlite file" );
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
// Create the coordinator and store
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreData2.sqlite"];
NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
// Report any error we got.
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
dict[NSLocalizedFailureReasonErrorKey] = failureReason;
dict[NSUnderlyingErrorKey] = error;
error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
// Replace this 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.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
- (NSManagedObjectContext *)managedObjectContext {
NSLog(@"[self managedObjectContext] called, (NSManagedObjectContext *)managedObjectContext - scratch pad for objects that come from the database" );
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;
}
#pragma mark - Core Data Saving support
- (void)saveContext {
NSLog(@" --- saveContext --- " );
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
NSError *error = nil;
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&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.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}