我正在學習核心數據 如何使用核心數據刪除和更新數據。我曾經在上傳和獲取。我需要編寫刪除和更新的方法。請告訴我如何使用核心數據更新和刪除重新編碼。如何使用核心數據更新和刪除需要記錄的表格
下面我編寫了使用核心數據從表中上載和提取數據的代碼。
for (NSManagedObject *managedObject in items) {
[self.managedObjectContext deleteObject:managedObject];
DebugLog(@"%@ object deleted",entityDescription);
}
if (![self.managedObjectContext save:&error]) {
DebugLog(@"Error deleting %@ - error:%@",entityDescription,error);
}
更新以類似的方式進行,取對象需要,更改:
-(void)uploadData
{
Employee *empObj=(Employee *)[NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
empObj.empId=[NSNumber numberWithInt:12345];
empObj.empSalary=[NSNumber numberWithInt:25000];
[email protected]"Venu";
[email protected]"Programmer";
[email protected]"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============================================================================= ");
}
}
這裏會發生什麼?是否足夠聰明地保存「更改記錄」或更新empArray中的所有項目? – frankish 2014-07-03 20:20:13