Overview:
.
- (void)removeEmployees:(NSSet *)values; //Auto generated method in Department.h
However adding of an NSManagedObject using the add method allows the database to be saved successfully
- (void)addEmployeesObject:(Employee *)value; //Auto generated method in Department.h - works fine
Note: I am using NSManagedObject subclasses generated by XCode.
Entity Details:
Relationship Details:
Problem:
"Department" class has a method called as mentioned below, after I use this method, the database save is not successful
(void)removeEmployees:(NSSet *)values; //Auto generated method in Department.h //does not work
Code used to Delete:
- (void) removeEmployeesHavingAgeAsZeroWithDepartment: (Department*) department
{
NSMutableSet *employeesSetToBeRemoved = [[NSMutableSet alloc] init];
for(Employees *currentEmployee in department.employees)
{
if(currentEmployee.age == 0)
{
[employeesSetToBeRemoved addObject:currentEmployee];
}
}
[department removeEmployees:employeesSetToBeRemoved]; //causing the problem
}
Code used to save the database
[self.database saveToURL:self.database.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success)
{NSLog(@"DB saved");}];
Questions:
Answer:
After some investigation, I realized that the database was not getting saved.
Instead of using the auto generated remove method, I used the NSManagedObjectContext
's deleteObject
method
Fix:
[self.managedObjectContext deleteObject:currentEmployee];