I am learning the core data
How to delete and update the data using core data. I have worked on upload and fetch. I need to write the methods for delete and update. Please tell me how to update and delete a recode using core data.
Below I have written code for upload and fetch data from table using core data.
-(void)uploadData
{
Employee *empObj=(Employee *)[NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
empObj.empId=[NSNumber numberWithInt:12345];
empObj.empSalary=[NSNumber numberWithInt:25000];
empObj.empName=@"Venu";
empObj.empDesignation=@"Programmer";
empObj.empExp=@"2 Years";
if ([self.managedObjectContext hasChanges] )
{
[self.managedObjectContext save:nil];
}
}
-(void)fetchData
{
NSEntityDescription *empEntity = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
// Setup the fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
NSArray *fatherArray=[self.managedObjectContext executeFetchRequest:request error:nil];
[request setEntity:empEntity];
NSArray *empArray=[self.managedObjectContext executeFetchRequest:request error:nil];
for (int i=0; i<[empArray count]; i++)
{
printf("\n=================================Recored== %d==================================== ",i);
Employee *empObj=[empArray objectAtIndex:i];
printf("\n obj.empName========= %s",[empObj.empName UTF8String]);
printf("\n obj.empDesignation========= %s",[empObj.empDesignation UTF8String]);
printf("\n obj.empExp========= %s",[empObj.empExp UTF8String]);
printf("\n obj.empId========= %d",[empObj.empId intValue]);
printf("\n obj.empSalary========= %d",[empObj.empSalary intValue]);
printf("\n============================================================================= ");
}
}
I am doing a project which involved in Core Data, and I would
like to share something with you about it.
It is a clear that before you delete or update a record you need
to retrieve that record.
Use the employee with empId 12345 as an example,
a)Delete
b) update