Skip to content

Commit

Permalink
Resolved CoreData issue
Browse files Browse the repository at this point in the history
  • Loading branch information
niitMobileLab authored and niitMobileLab committed May 10, 2016
1 parent f5602d1 commit 0718295
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
1 change: 1 addition & 0 deletions EmployeeDirectory/LoginViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
-(void)showNextScreen;
-(void)validateCredentials:(NSString*)userName password:(NSString*)pwd;
-(void)showAlert:(NSString*)msg;
-(BOOL)HasEmployeeData;

@end
52 changes: 35 additions & 17 deletions EmployeeDirectory/LoginViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,47 @@ -(void)showNextScreen
// Saves Employee Data in Model Object
-(void)setUpData:(NSDictionary *)employeeData
{
NSDictionary * employeeDict = [employeeData objectForKey:@"employees"];

for (NSDictionary *emp in employeeDict )
if(![self HasEmployeeData])
{
Employee *employee = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:[[Utility sharedManager]managedObjectContext]];
NSDictionary * employeeDict = [employeeData objectForKey:@"employees"];

for (NSDictionary *emp in employeeDict )
{
Employee *employee = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:[[Utility sharedManager]managedObjectContext]];

employee.id = [NSNumber numberWithInt:[[emp valueForKey:@"id"] intValue]];
employee.firstName = emp[@"firstName"];
employee.lastName = emp[@"lastName"];
employee.title = emp[@"title"];
employee.managerId = [NSNumber numberWithInt:[[emp valueForKey:@"managerId"] intValue]];
employee.officePhone = emp[@"officePhone"];
employee.cellPhone = emp[@"cellPhone"];
employee.email = emp[@"email"];
employee.picture = emp[@"picture"];
NSError *error;
if (![[[Utility sharedManager]managedObjectContext] save:&error]) {
NSLog(@"Save Error: %@", [error localizedDescription]);
}

employee.id = [NSNumber numberWithInt:[[emp valueForKey:@"id"] intValue]];
employee.firstName = emp[@"firstName"];
employee.lastName = emp[@"lastName"];
employee.title = emp[@"title"];
employee.managerId = [NSNumber numberWithInt:[[emp valueForKey:@"managerId"] intValue]];
employee.officePhone = emp[@"officePhone"];
employee.cellPhone = emp[@"cellPhone"];
employee.email = emp[@"email"];
employee.picture = emp[@"picture"];
NSError *error;
if (![[[Utility sharedManager]managedObjectContext] save:&error]) {
NSLog(@"Error: %@", [error localizedDescription]);
}

}
}


-(BOOL)HasEmployeeData
{
NSManagedObjectContext *moc = [[Utility sharedManager]managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:moc];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entityDescription];

NSError *error;
NSArray *employees = [moc executeFetchRequest:fetchRequest error:&error];
return [employees count] >0 ? YES: NO;

}

/* In a storyboard-based application, you will often want to do a little preparation before navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation// In a storyboard-based application, you will often want to do a little preparation before navigation// In a storyboard-based application, you will often want to do a little preparation before navigation// In a storyboard-based application, you will often want to do a little preparation before navigation// In a storyboard-based application, you will often want to do a little preparation before navigation// In a storyboard-based application, you will often want to do a little preparation before navigation// In a storyboard-based application, you will often want to do a little preparation before navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation// In a storyboard-based application, you will often want to do a little preparation before navigation// In a storyboard-based application, you will often want to do a little preparation before navigation// In a storyboard-based application, you will often want to do a little preparation before navigation// In a storyboard-based application, you will often want to do a little preparation before navigation// In a storyboard-based application, you will often want to do a little preparation before navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
Expand Down

0 comments on commit 0718295

Please sign in to comment.