I have a 1-to-many relationship in a Core Data graph, and I'm trying to understand the difference between using a CoreDataGeneratedAccessors method and a simple assignment to alter a relationship. For example, the Core Data Programming Guide has a department and employee example. In that example they use the CoreDataGeneratedAccessors to hire and fire employees:
[aDepartment addEmployeesObject:newEmployee];
[aDepartment removeEmployeesObject:firedEmployee];
They don't define an inverse relationship, but say "department" is the inverse relationship to "employees". Should the following then accomplish the same thing?
newEmployee.department = aDepartment
firedEmployee.department = nil;
According to the Manipulating Relationships and Object Graph Integrity section of the Core Data Programming Guide, the later examples should automatically fix all relationships to maintain graph consistency. If that's the case, is there any reason to use the CoreDataGeneratedAccessors when an inverse relationship exists? Does using the CoreDataGeneratedAccessors maintain graph consistency on inverse relationships?
They don't define an inverse relationship, but say "department" is the inverse relationship to "employees". Should the following then accomplish the same thing?
Both operations have the same result no matter which end of the relationship (with inverse) you modify.
If that's the case, is there any reason to use the CoreDataGeneratedAccessors when an inverse relationship exists? Does using the CoreDataGeneratedAccessors maintain graph consistency on inverse relationships?
Consistency is not an issue with both methods. For performance reasons it is very important to modify large relationships with apropriate methods.
Solution 1 (firing all employees of a department)
for (Employee* employee in aDepartment.employees)
{
employee.department = nil
}
Solution 2
aDepartment.employees = nil;
The first solution would trigger an update of a (table-)view after each operation while the second would result in exactly one update of all views. This can be a big difference if you handle a a large amount of objects.
If you need more in depth information, I think similar topics have already been discussed on SO.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With