// BEGINNING OF CORE DATA SECTION
-(void)printContext:(NSManagedObject *) managedObj
{
/*
NSString *courierID, *emailAddress, *loggedIn, *timeLoggedIn;
courierID = [NSString stringWithFormat:@"%@",[[managedObj valueForKey:@"courierID"]stringValue]];
emailAddress = [managedObj valueForKey:@"emailAddress"];
loggedIn = [[managedObj valueForKey:@"loggedIn"] stringValue];
timeLoggedIn = [NSDateFormatter localizedStringFromDate:[managedObj valueForKey:@"timeLoggedIn"]
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterFullStyle];
NSLog(@"GFMVC: Core Data Courier ID: %@", courierID);
NSLog(@"GFMVC: Core Data Email Address: %@", emailAddress);
NSLog(@"GFMVC: Core Data Logged In: %@", loggedIn);
NSLog(@"GFMVC: Core Data Time Logged In: %@", timeLoggedIn);
*/
return;
}
- (NSFetchedResultsController *)fetchedResultsController:(NSString *) entityName
{
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:entityName inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
/*
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"emailAddress" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
*/
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext sectionNameKeyPath:nil
cacheName:@"Root"];
fetchedResultsController = theFetchedResultsController;
// self.fetchedResultsController.delegate = self;
return fetchedResultsController;
}
-(void)saveContext:(NSManagedObjectContext *) managedObjContext
{
if (managedObjContext == nil) {
managedObjContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSLog(@"After managedObjectContext: %@", managedObjContext);
}
NSError *error = nil;
if (![managedObjContext save:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
-(void)setDataPoint:(NSManagedObject *) managedObj setManagedObjectContext: (NSManagedObjectContext* ) context setValue: (NSString*) valueString setKey:(NSString *) keyValue
{
[managedObj setValue:valueString forKey:keyValue];
[self saveContext:context];
NSLog(@"D2D: OrdersViewController: Set Data Point %@: %@", keyValue, valueString);
return;
}
-(NSString *)getDataPoint:(NSManagedObject *) managedObj setKey:(NSString *) keyString
{
NSString *keyValue;
if(managedObj != NULL)
{
keyValue = [managedObj valueForKey:keyString];
}
else
{
keyValue = @"";
}
NSLog(@"D2D: Core Data %@: %@", keyString, keyValue);
return keyValue;
}
-(NSArray *)getArray:(NSManagedObjectContext *)context setEntityName:(NSString*) entityName
{
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:entityName
inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];
return results;
}
-(NSManagedObject *) getManagedObject:(NSArray *) results setContext:(NSManagedObjectContext *) context
setEntityName:(NSString *)entityName
{
NSManagedObject *newManagedObject = nil;
if (results == nil)
{ // g
newManagedObject = [NSEntityDescription
insertNewObjectForEntityForName:entityName
inManagedObjectContext:context];
// set all the values for the object when I get home.
NSLog(@"getManagedObject Point 1");
}
else if (results != nil && [results count] == 1) {
newManagedObject = [results objectAtIndex:0];
NSLog(@"getManagedObject Point 2");
}
else if (results != nil && [results count] == 0) {
newManagedObject = [NSEntityDescription
insertNewObjectForEntityForName:entityName
inManagedObjectContext:context];
NSLog(@"getManagedObject Point 3");
} // g
return newManagedObject;
}
-(NSManagedObjectContext *) getManagedObjectContext
{
NSManagedObjectContext* context = [fetchedResultsController managedObjectContext];
if (context == nil)
{ // f
context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSLog(@"After managedObjectContext: %@", context);
} // f
return context;
}
// END OF CORE DATA SECTION