Skip to content

Instantly share code, notes, and snippets.

@patelrohan
Created March 2, 2012 10:11
Show Gist options
  • Save patelrohan/1957502 to your computer and use it in GitHub Desktop.
Save patelrohan/1957502 to your computer and use it in GitHub Desktop.
Core data
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title=@"Home";
segmentToggle = segmentedControl.selectedSegmentIndex;
//-------------
if (managedObjectContext == nil)
{
managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSLog(@"After managedObjectContext: %@", managedObjectContext);
}
// NSManagedObjectContext *context=[self managedObjectContext];
FailedBankInfo *failedBankInfo = [NSEntityDescription insertNewObjectForEntityForName:@"FailedBankInfo" inManagedObjectContext:managedObjectContext];
failedBankInfo.name = @"Test Bank";
failedBankInfo.city = @"Testville";
failedBankInfo.state = @"Testland";
FailedBankDetails *failedBankDetails = [NSEntityDescription
insertNewObjectForEntityForName:@"FailedBankDetails"
inManagedObjectContext:managedObjectContext];
failedBankDetails.closeDate = [NSDate date];
failedBankDetails.updatedDate = [NSDate date];
failedBankDetails.zip = [NSNumber numberWithInt:12345];
failedBankDetails.info = failedBankInfo;
failedBankInfo.details = failedBankDetails;
NSError *error;
if (![managedObjectContext save:&error])
{
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
// Test listing all FailedBankInfos from the store
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"FailedBankInfo"
inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
for (FailedBankInfo *info in fetchedObjects)
{
NSLog(@"Name: %@", info.name);
FailedBankDetails *details = info.details;
NSLog(@"Zip: %@", details.zip);
}
[fetchRequest release];
//---------------
NSFetchRequest *fetchRequest1 = [[NSFetchRequest alloc] init];
NSEntityDescription *entity1 = [NSEntityDescription
entityForName:@"FailedBankInfo" inManagedObjectContext:managedObjectContext];
[fetchRequest1 setEntity:entity1];
NSError *error1;
self.failedBankInfos = [managedObjectContext executeFetchRequest:fetchRequest1 error:&error1];
self.title = @"Failed Banks";
[fetchRequest1 release];
// Do any additional setup after loading the view from its nib.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment