Core Data Relationships

Notice that when you delete a company any people who are employees of that company also get deleted.  This is managed by setting the relationship in the Core Data model to Cascade deletes. Realistically you probably wouldn’t want to do that. Perhaps for an invoice->invoiceItem relationship you might.

If you run the OS X app and delete an employee you will notice that the person does not get deleted.  This is because OS X UI controls are smart enough to realise that you probably only want to remove them as an employee from the company so it sets the persons employer attribute to NIL.  In the People table you will see the employer column becomes empty.

Take care when designing your model to make sure it works correctly with your UI objects.

Now try deleting an Employee from the iOS App – see what happens, the person gets deleted.  Look at the detailViewController code and notice that we are deleting the person object and not just removing the person from the companies employees list.

FIX: instead of deleting the person object change the code to simply set the persons employer to NIL. Replace this line of code

[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];

with this line

[[self.fetchedResultsController objectAtIndexPath:indexPath] setValue:(nil) forKey:@"company"];

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s